Skip to content

Instantly share code, notes, and snippets.

@kbrandwijk
Last active October 31, 2017 01:20
Show Gist options
  • Save kbrandwijk/47a6479d9668153451eb6272818b8aa9 to your computer and use it in GitHub Desktop.
Save kbrandwijk/47a6479d9668153451eb6272818b8aa9 to your computer and use it in GitHub Desktop.
Graphcool API Gateway configuration
endpoints:
- url: http://localhost:60000/simple/v1/cj9bgncpa00040157l0nft8x4
authentication: caller
- url: ${env:GRAPHCOOL_SECOND_ENDPOINT}
authentication:
token: ${env:GRAPHCOOL_TOKEN}
types:
- sdl: ./src/linkingSchema.graphql
- code: ./src/permissionSchema.ts
functions:
Query:
myUser:
src: ./src/myUserResolver.ts
topPosts:
webhook: https://mywebhookurl
customResolvers:
- ./src/generatedResolvers.ts
# transformations = input for transformSchema
transformations:
Query:
allPosts: false
allUsers: false
topPosts: "myTopPosts" # rename
Mutation:
deleteUser: false # hide
updateUser: ./src/somefunctions.ts # see transformSchema docs
options:
usePlayground: true
tracing:
apolloEngine:
key: ${env:APOLLO_ENGINE_KEY}
import * as inflector from 'inflector-js'
const TYPES = ['User', 'Address']
export default mergeInfo => ({
Query: TYPES.reduce((result, typeName) => {
result[`Some${typeName}Exists`] = {
async resolve(parent, args, context, info) {
const newInfo = JSON.parse(JSON.stringify(info));
const idField = { kind: 'Field', name: { kind: 'Name', value: 'id' } }
newInfo.fieldNodes[0].selectionSet = { kind: 'SelectionSet', selections: [idField]}
const result = await mergeInfo.delegate(
'query', `all${inflector.pluralize(typeName)}`, { args }, context, newInfo
)
return result.length > 0
}
}
return result
}, {})
})
import { DocumentNode, GraphQLResolveInfo, OperationDefinitionNode, parse, FieldNode } from 'graphql'
/*
mergeInfo.delegate only allows you to pass in an operationName and variables.
It does not allow you to define query fields, because they are inferred from the user query.
If you need to delegate a query that is unrelated to the user query, you need to provide
the fields you need. This helper does that, based on a provided query.
*/
export const delegateHelper = mergeInfo => ({
fromQuery: async (
query: string,
args: { [key: string]: any },
context: { [key: string]: any },
info: GraphQLResolveInfo) => {
const document: DocumentNode = parse(query)
const operationDefinition: OperationDefinitionNode = document.definitions[0] as OperationDefinitionNode
const operationType:string = operationDefinition.operation
const operationName:string = (operationDefinition.selectionSet.selections[0] as any).name.value
const fields: [FieldNode] = (operationDefinition.selectionSet.selections[0] as any).selectionSet.selections
console.log(fields)
const newInfo: GraphQLResolveInfo = JSON.parse(JSON.stringify(info));
console.log(newInfo.fieldNodes[0])
newInfo.fieldNodes[0].selectionSet!.selections = fields
return await mergeInfo.delegate(
operationType, operationName, { }, context, newInfo
)
}
})
extend type Query {
myUser: MyUser
}
import { delegateHelper } from './helpers'
export default async (event) => {
const query = `
query {
validateToken {
nodeId
typeName
}
}`
const auth = await delegateHelper(event.mergeInfo).fromQuery(query, {}, event.context, event.info)
if (auth.typeName === 'MyUser') {
return event.mergeInfo.delegate(
'query', 'MyUser', { id: auth.nodeId }, event.context, event.info
)
}
return null
}
const TYPES = ['User', 'Address']
export const schema = `
extend type Query {
${TYPES
.map(t => `Some${t}Exists(filter: ${t}Filter): Boolean`)
.join('\n')}
}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment