Skip to content

Instantly share code, notes, and snippets.

@huysentruitw
Last active February 24, 2020 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huysentruitw/84cb6f153b2845079bbb94ef739bca06 to your computer and use it in GitHub Desktop.
Save huysentruitw/84cb6f153b2845079bbb94ef739bca06 to your computer and use it in GitHub Desktop.
Nested graphql resolvers with NodeJS
"use strict";
const express = require('express');
const express_graphql = require('express-graphql');
const { GraphQLSchema, GraphQLObjectType, GraphQLString } = require('graphql');
// GraphQL schema
const songType = new GraphQLObjectType({
name: 'SongType',
fields: {
name: {
type: GraphQLString,
}
}
})
const spotifyType = new GraphQLObjectType({
name: 'SpotifyType',
fields: {
getCurrentSong: {
type: songType,
resolve: () => ({
name: 'Armin van Buuren - Blah Blah Blah'
}),
},
},
});
const githubRecentActivityType = new GraphQLObjectType({
name: 'GitHubRecentActivityType',
fields: {
description: {
type: GraphQLString,
},
}
})
const githubType = new GraphQLObjectType({
name: 'GitHubType',
fields: {
getRecentActivity: {
type: githubRecentActivityType,
resolve: () => ({
description: 'Some activity',
}),
}
},
});
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: {
spotify: {
type: spotifyType,
resolve: () => ({}),
},
github: {
type: githubType,
resolve: () => ({}),
},
},
}),
});
// Express
const app = express();
app.use('/graphql', express_graphql({
schema: schema,
}));
app.listen(4000, () => console.log('Express GraphQL Server now running on http://localhost:4000/graphql'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment