Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add custom path into GQL constructor #104

Merged
merged 1 commit into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ __fastify-gql__ supports the following options:
executed before being jit'ed.
* `routes`: boolean. Serves the Default: `true`. A graphql endpoint is
exposed at `/graphql`.
* `path`: string. Change default graphql `/graphql` route to another one.
* `context`: `Function`. Result of function is passed to resolvers as a custom GraphQL context. The function receives the `request` and `reply` as parameters. It is only called when `routes` options is `true`
* `prefix`: String. Change the route prefix of the graphql endpoint if enabled.
* `defineMutation`: Boolean. Add the empty Mutation definition if schema is not defined (Default: `false`).
Expand Down
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ declare namespace FastifyGQL {
* @default true
*/
routes?: boolean,
/**
* An endpoint for graphql if routes is true
* @default '/graphql'
*/
path?: string,
/**
* Change the route prefix of the graphql endpoint if set
*/
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ module.exports = fp(async function (app, opts) {
errorHandler: opts.errorHandler,
ide: optsIde,
prefix: opts.prefix,
path: opts.path,
context: opts.context,
schema,
subscription: opts.subscription
Expand Down
6 changes: 4 additions & 2 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ module.exports = async function (app, opts) {
subscriber = new PubSub(emitter)
}

const { path: graphqlPath = '/graphql' } = opts

const getOptions = {
url: '/graphql',
url: graphqlPath,
method: 'GET',
schema: {
querystring: {
Expand Down Expand Up @@ -149,7 +151,7 @@ module.exports = async function (app, opts) {
app.route(getOptions)
}

app.post('/graphql', {
app.post(graphqlPath, {
schema: {
body: {
type: 'object',
Expand Down
36 changes: 36 additions & 0 deletions test/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,42 @@ test('POST route', async (t) => {
})
})

test('custom route', async (t) => {
const app = Fastify()
const schema = `
type Query {
add(x: Int, y: Int): Int
}
`

const resolvers = {
add: async ({ x, y }) => x + y
}

app.register(GQL, {
schema,
resolvers,
path: '/custom'
})

const query = '{ add(x: 2, y: 2) }'

const res = await app.inject({
method: 'POST',
url: '/custom',
body: {
query
}
})

t.equal(res.statusCode, 200)
t.deepEqual(JSON.parse(res.body), {
data: {
add: 4
}
})
})

test('GET route', async (t) => {
const app = Fastify()
const schema = `
Expand Down