forked from maticzav/graphql-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: Prevent application of middleware to introspection types.
BREAKING CHANGE: This version no longer wraps introspection types in middleware. Fixes maticzav#75 I believe this is a meaningful step because we shouldn't rely on changing introspection query to gain additional functionality. From my experience, this has been extensively used in libraries as `graphql-shield` which prevented the introspection of the schema. Nevertheless, I have a strong belief that robust systems shouldn't rely on hiding fields and types, but instead create a meaningfully rounded permission system. I suggest you use prior versions up to `3.0.0` (`2.x.x`) in case your application heavily relies on the wrapping of introspection types. I have also added integration tests for `ApolloServer` and `GraphQL Yoga`.
- Loading branch information
Showing
8 changed files
with
1,420 additions
and
110 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import test from 'ava' | ||
import { makeExecutableSchema } from 'graphql-tools' | ||
import { GraphQLServer as YogaServer } from 'graphql-yoga' | ||
import { gql, ApolloServer } from 'apollo-server' | ||
import * as request from 'request-promise-native' | ||
import { AddressInfo } from 'ws' | ||
import { applyMiddleware, middleware, IMiddleware } from '../' | ||
|
||
test('Works with GraphQL Yoga', async t => { | ||
const typeDefs = ` | ||
type Query { | ||
test: String! | ||
} | ||
` | ||
|
||
const resolvers = { | ||
Query: { | ||
test: () => 'test', | ||
}, | ||
} | ||
|
||
const schema = makeExecutableSchema({ typeDefs, resolvers }) | ||
|
||
const schemaWithMiddleware = applyMiddleware(schema, async resolve => { | ||
const res = await resolve() | ||
return `pass-${res}` | ||
}) | ||
|
||
const server = new YogaServer({ | ||
schema: schemaWithMiddleware, | ||
}) | ||
|
||
const http = await server.start({ port: 0 }) | ||
const { port } = http.address() as AddressInfo | ||
const uri = `http://localhost:${port}/` | ||
|
||
/* Tests */ | ||
|
||
const query = ` | ||
query { | ||
test | ||
} | ||
` | ||
|
||
const body = await request({ | ||
uri, | ||
method: 'POST', | ||
json: true, | ||
body: { query }, | ||
}).promise() | ||
|
||
t.deepEqual(body, { | ||
data: { | ||
test: 'pass-test', | ||
}, | ||
}) | ||
}) | ||
|
||
test('Works with ApolloServer', async t => { | ||
const typeDefs = ` | ||
type Query { | ||
test: String! | ||
} | ||
` | ||
|
||
const resolvers = { | ||
Query: { | ||
test: () => 'test', | ||
}, | ||
} | ||
|
||
const schema = makeExecutableSchema({ typeDefs, resolvers }) | ||
|
||
const schemaWithMiddleware = applyMiddleware(schema, async resolve => { | ||
const res = await resolve() | ||
return `pass-${res}` | ||
}) | ||
|
||
const server = new ApolloServer({ | ||
schema: schemaWithMiddleware, | ||
}) | ||
|
||
await server.listen({ port: 8008 }) | ||
const uri = `http://localhost:8008/` | ||
|
||
/* Tests */ | ||
|
||
const query = ` | ||
query { | ||
test | ||
} | ||
` | ||
|
||
const body = await request({ | ||
uri, | ||
method: 'POST', | ||
json: true, | ||
body: { query }, | ||
}).promise() | ||
|
||
t.deepEqual(body, { | ||
data: { | ||
test: 'pass-test', | ||
}, | ||
}) | ||
}) |
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
Oops, something went wrong.