-
-
Save lorefnon/2a62dc5722e924632e9fe391723df5e5 to your computer and use it in GitHub Desktop.
Garph: internal-in-input-type
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
import { g, InferResolvers, buildSchema, Infer } from './../src/index' | |
import { createYoga } from 'graphql-yoga' | |
import { createServer } from 'http' | |
const addrType = g.type('Address', { | |
id: g.id(), | |
name: g.string(), | |
}) | |
const addrFilterType = g.inputType('AddrFilter', { | |
name: g.string(), | |
id: g.internal<string>(), // <---- Use of internal prop inside an input type | |
}) | |
const personType = g.type('Person', { | |
name: g.string(), | |
id: g.internal<number>(), | |
address: g.ref(addrType) | |
.list() | |
.omitResolver() | |
.args({ | |
filter: addrFilterType | |
}), | |
}) | |
const queryType = g.type('Query', { | |
greet: g.ref(personType) | |
.args({ | |
name: g.string().optional().default('Max'), | |
}) | |
.description('Greets a person') | |
}) | |
type QueryType = Infer<typeof queryType> | |
const resolvers: InferResolvers<{ | |
Query: typeof queryType, | |
Person: typeof personType, | |
}, {}> = { | |
Query: { | |
greet: (parent, args, context, info) => { | |
return { | |
name: `Hello, ${args.name}`, | |
id: 123 | |
} | |
} | |
}, | |
Person: { | |
address: (parent, args) => { | |
return [{ | |
id: args.filter.id, // <----- Will fail at runtime, even though ts is happy | |
name: "Home sweet home" | |
}] | |
} | |
} | |
} | |
const schema = buildSchema({ g, resolvers }) | |
const yoga = createYoga({ schema }) | |
const server = createServer(yoga) | |
server.listen(4000, () => { | |
console.info('Server is running on http://localhost:4000/graphql') | |
}) |
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
query { | |
greet { | |
address(filter:{name:"hello"}) { | |
id | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment