Last active
February 24, 2020 19:38
-
-
Save huysentruitw/84cb6f153b2845079bbb94ef739bca06 to your computer and use it in GitHub Desktop.
Nested graphql resolvers with NodeJS
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
"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