Last active
April 11, 2017 09:32
-
-
Save parwatcodes/561bbc386c33e2d448ca8962e40ae405 to your computer and use it in GitHub Desktop.
This file contains 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
const fakeDatabase = [ | |
{ | |
id: 1, | |
name: 'Abhay', | |
description: 'This is Abhay\'s Database' | |
}, | |
{ | |
id: 2, | |
name: 'Bankimchandra', | |
description: 'This is Bankimchandra\'s Database' | |
}, | |
{ | |
id: 3, | |
name: 'chandu', | |
description: 'This is chandu\'s Database' | |
} | |
] | |
module.exports = fakeDatabase; |
This file contains 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
const graphql = require('graphql'); | |
const fakeDatabase = require('./fakeDatabase'); | |
const fakeDatabaseType = new graphql.GraphQLObjectType({ | |
name: 'fakeDatabase', | |
fields: { | |
id: { type: graphql.GraphQLID }, | |
name: { type: graphql.GraphQLString }, | |
description: { type: graphql.GraphQLString }, | |
}, | |
}); | |
var schema = {}; | |
schema.getAllUser = new graphql.GraphQLObjectType({ | |
name: 'getAllUser', | |
fields: { | |
data: { | |
type: new graphql.GraphQLList(fakeDatabaseType), | |
resolve: () => { | |
console.log('from get', fakeDatabase) | |
return fakeDatabase; // here, as our list have elements {} with id, name and description, you need to pass the data from | |
// the own resolver of type data as it is the parent for fakeDatabaseType | |
} | |
} | |
} | |
}) | |
const queryType = new graphql.GraphQLObjectType({ | |
name: 'Query', | |
fields: { | |
getAllUser: { | |
type: schema.getAllUser, | |
args: { | |
}, resolve: function () { | |
console.log('fromm query', fakeDatabase); | |
return {}; // just passing empty object, if i have to send the fakeDatabase from here then our data must contain field data, | |
} | |
} | |
} | |
}); | |
schema.queryTypq1 = new graphql.GraphQLSchema({ query: queryType }); | |
var app = require('express')(); | |
var graphHTTP = require('express-graphql'); | |
app.use('/graphql', graphHTTP({ | |
schema: schema.queryTypq1, | |
graphiql: true | |
})); | |
app.listen(4000, () => { console.log('Server is running on port: 4000'); }); |
Author
parwatcodes
commented
Apr 11, 2017
Thank You so much, It worked for me. You saved my time. I really want to appreciate york effort and time. Best wishes.
But tell me one thing-
why we are using resolve in schema.getAllUser(){ ..... } ?
/schema.getAllUser = new graphql.GraphQLObjectType({
name: 'getAllUser',
fields: {
data: {
type: new graphql.GraphQLList(fakeDatabaseType),
resolve: () => {
return fakeDatabase; // why here though we are also using resolve() in the resolver method.
}
}
}
})
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment