GraphQL links

node v6.17.1
version: 1.0.1
endpointsharetweet
Dependencies
var { graphql, GraphQLSchema, GraphQLObjectType, GraphQLString, GraphQLNonNull, GraphQLInterfaceType, GraphQLList, } = require('graphql');
Link schema
var Link = new GraphQLObjectType({ name: 'link', fields: () => ({ id: { type: GraphQLString, }, source: { type: Link, }, target: { type: Link, }, }), });
REST api for links
var Query = new GraphQLObjectType({ name: 'query', fields: { link: { type: Link, args: { id: { type: new GraphQLNonNull(GraphQLString), }, }, }, links: { type: new GraphQLList(Link) }, add: { type: Link, args: { id: { type: GraphQLString, }, source: { type: GraphQLString, }, target: { type: GraphQLString, }, }, }, update: { type: Link, }, }, });
adapter part resolvers, root schema definition, fake database
var schema = new GraphQLSchema({ query: Query, }); // fake database var links = []; var resolvers = {}; resolvers.add = (args) => { args.id = links.length; links.push(args); return resolvers.link(args); }; resolvers.link = (args) => { if (links[args.id]) { return { id: () => links[args.id].id, source: () => resolvers.link({ id: links[args.id].source }), target: () => resolvers.link({ id: links[args.id].target }), }; } }; resolvers.links = () => links.map((link) => resolvers.link(link));
requests
await graphql(schema, ` { add { id } } `, resolvers).then(); await graphql(schema, ` { add(source: "0") { id } } `, resolvers).then(); await graphql(schema, ` { add(source: "1" target: "2") { id source { id } target { id } } } `, resolvers).then();
await graphql(schema, ` { link(id: "2") { id source { id } target { id } } } `, resolvers).then();
await graphql(schema, ` { links { ...link } } fragment link on link { id source { id } target { id } } `, resolvers).then();
Loading…

no comments

    sign in to comment