Created
November 25, 2019 18:13
-
-
Save lorefnon/cf5aecedc0ef98fc384e9091039817ad to your computer and use it in GitHub Desktop.
GRelDAL custom operations without Database interactions
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 { mapSchema } = require("greldal"); | |
const { graphql, GraphQLString } = require("graphql"); | |
const customOperation = { | |
operationType: "query", | |
name: "printHello", | |
fieldConfig: { | |
type: GraphQLString, | |
description: "Prints hello", | |
resolve: (obj, args, context, info) => { | |
return "hello"; | |
} | |
} | |
} | |
const customOperationWithArgs = { | |
operationType: "query", | |
name: "printGreeting", | |
fieldConfig: { | |
type: GraphQLString, | |
args: { | |
name: { | |
type: GraphQLString, | |
defaultValue: "Markus" | |
} | |
}, | |
description: "Prints hello", | |
resolve: (obj, args, context, info) => { | |
return `hello ${args.name}`; | |
} | |
} | |
} | |
const generatedSchema = mapSchema([ | |
customOperation, | |
customOperationWithArgs | |
]); | |
(async () => { | |
const result1 = await graphql(generatedSchema, ` | |
query { | |
printHello | |
} | |
`); | |
console.log('result1: ', result1); | |
const result2 = await graphql(generatedSchema, ` | |
query { | |
printGreeting | |
} | |
`); | |
console.log('result2: ', result2); | |
const result3 = await graphql(generatedSchema, ` | |
query { | |
printGreeting(name: "Lorefnon") | |
} | |
`); | |
console.log('result3: ', result3); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment