-
Notifications
You must be signed in to change notification settings - Fork 88
Integrate graphql crunch #1042
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
Integrate graphql crunch #1042
Changes from 4 commits
f02ff6d
d13c2f7
aedeaa1
0fecece
cde0fb4
c31077b
db5eefe
5bfd88d
30312d6
9fbac29
60d25fd
f5cec6c
8b5bbb9
27314cd
6fdd18a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import express from "express" | ||
| import graphqlHTTP from "express-graphql" | ||
| import request from "supertest" | ||
| import bodyParser from "body-parser" | ||
| import { makeExecutableSchema, addMockFunctionsToSchema } from "graphql-tools" | ||
| import { crunch } from "graphql-crunch" | ||
|
|
||
| import crunchInterceptor from "../crunchInterceptor" | ||
|
|
||
| describe("crunchInterceptor", () => { | ||
| let app | ||
|
|
||
| beforeEach(() => { | ||
| const schema = makeExecutableSchema({ | ||
| typeDefs: ` | ||
| type Query { | ||
| greeting: String | ||
| } | ||
| `, | ||
| }) | ||
| addMockFunctionsToSchema({ schema }) | ||
| app = express() | ||
| app.use( | ||
| "/", | ||
| bodyParser.json(), | ||
| crunchInterceptor, | ||
| graphqlHTTP({ | ||
| schema, | ||
| graphiql: false, | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| it("should pass the result through unchanged when no param is present", () => { | ||
| return request(app) | ||
| .get("/?query={greeting}") | ||
| .set("Accept", "application/json") | ||
| .expect(res => { | ||
| expect(res.body.data).toMatchObject({ greeting: "Hello World" }) | ||
| }) | ||
| }) | ||
|
|
||
| it("should crunch the result when param is present", () => { | ||
| return request(app) | ||
| .get("/?query={greeting}&crunch") | ||
| .set("Accept", "application/json") | ||
| .expect(res => { | ||
| expect(res.body.data).toMatchObject(crunch({ greeting: "Hello World" })) | ||
| }) | ||
| }) | ||
|
|
||
| it("should not crunch an introspection query", () => { | ||
| return request(app) | ||
| .get("/?query={__schema{types{name}}}&crunch") | ||
| .set("Accept", "application/json") | ||
| .expect(res => { | ||
| expect(Array.isArray(res.body.data)).toBeFalsy() | ||
| }) | ||
| }) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { crunch } from "graphql-crunch" | ||
| import interceptor from "express-interceptor" | ||
|
|
||
| export default interceptor(req => ({ | ||
| isInterceptable: () => req.query.hasOwnProperty("crunch"), | ||
| intercept: (body, send) => { | ||
| body = JSON.parse(body) // eslint-disable-line no-param-reassign | ||
| if (body && body.data && !body.data.__schema) { | ||
| body.data = crunch(body.data) // eslint-disable-line no-param-reassign | ||
| } | ||
| send(JSON.stringify(body)) | ||
| }, | ||
| })) | ||
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The expectation is that a crunched result will be an array whereas a non-crunched result will be an object. This isn't a very sophisticated test, but it at least provides some level of validation. I'm not convinced this should even be a thing though... more on that below.