Created
September 7, 2015 08:41
-
-
Save mfirry/1ba61efcd31f7c744476 to your computer and use it in GitHub Desktop.
Foolin' around with GraphQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Two steps: | |
- babel MySchema.js > dist/MySchema.js | |
- node dist/MySchema.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
graphql, | |
GraphQLSchema, | |
GraphQLObjectType, | |
GraphQLString, | |
GraphQLEnumType, | |
GraphQLInterfaceType, | |
GraphQLList, | |
GraphQLNonNull, | |
GraphQLInt | |
} from 'graphql'; | |
// Initial store | |
var tickets = [{ | |
from: 'CTA', | |
via: 'FCO', | |
to: 'JFK', | |
airports: ['CTA', 'FCO', 'JFK'] | |
}, { | |
from: 'FCO', | |
via: null, | |
to: 'JFK', | |
airports: ['FCO', 'JFK'] | |
}, { | |
from: 'MXP', | |
via: null, | |
to: 'CIY', | |
airports: ['MXP', 'CIY'] | |
}]; | |
// Get function used in query | |
let getTicket = function(index) { | |
return tickets[index]; | |
}; | |
// Add function used in mutation | |
let add = function(from, to, via, airports) { | |
let _via = via ? via : null; | |
var ticket = { | |
from: from, | |
via: _via, | |
to: to, | |
airports: airports | |
}; | |
tickets.push(ticket); | |
return ticket; | |
}; | |
// Schema definitions | |
var ticketType = new GraphQLObjectType({ | |
name: 'Ticket', | |
description: 'A ticket for a flight from A to B', | |
fields: () => ({ | |
id: { | |
type: GraphQLString, | |
description: 'The id of the flight.' | |
}, | |
from: { | |
type: GraphQLString, | |
description: 'IATA CODE for the departing airport' | |
}, | |
via: { | |
type: GraphQLString, | |
description: 'IATA CODE for the connection airport' | |
}, | |
to: { | |
type: GraphQLString, | |
description: 'IATA CODE for the arriving airport' | |
}, | |
airports: { | |
type: new GraphQLList(GraphQLString), | |
description: 'List of involved airports' | |
} | |
}) | |
}); | |
// Query | |
var queryType = new GraphQLObjectType({ | |
name: 'Query', | |
fields: () => ({ | |
ticket: { | |
type: ticketType, | |
args: { | |
index: { | |
description: '', | |
type: GraphQLInt | |
} | |
}, | |
resolve: (root, { index }) => getTicket(index), | |
} | |
}) | |
}); | |
// Mutation | |
var mutationType = new GraphQLObjectType({ | |
name: 'RootMutationType', | |
fields: () => ({ | |
add: { | |
type: ticketType, | |
args: { | |
from: { | |
description: '', | |
type: GraphQLString | |
}, | |
via: { | |
description: '', | |
type: GraphQLString | |
}, | |
to: { | |
description: '', | |
type: GraphQLString | |
}, | |
airports: { | |
description: '', | |
type: new GraphQLList(GraphQLString) | |
} | |
}, | |
resolve: (root, { from, to, via, airports }) => add(from, to, via, airports), | |
} | |
}) | |
}); | |
var schema = new GraphQLSchema({ | |
query: queryType, | |
mutation: mutationType | |
}); | |
var query = '{ ticket(index: 1) { from to } }'; | |
var mutationAdd = 'mutation M { add(from: "LIN" to: "FCO" airports: ["FCO", "LIN"]) { from via to airports } }' | |
// Query example | |
// graphql(schema, query).then(result => { | |
// console.log(result); | |
// console.log("------"); | |
// console.log(tickets); | |
// }); | |
// Mutation example | |
graphql(schema, mutationAdd).then(result => { | |
console.log(result); | |
console.log("------"); | |
console.log(tickets); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "test-graphql", | |
"version": "1.0.0", | |
"dependencies": { | |
"graphql": "^0.4.3" | |
}, | |
"devDependencies": { | |
"babel": "^5.8.23" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment