Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(stitch): add a reproduction for issue #1571
- Loading branch information
1 parent
ace3f46
commit aebc42d
Showing
1 changed file
with
125 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { linkToExecutor } from '@graphql-tools/links'; | ||
import { makeExecutableSchema } from '@graphql-tools/schema'; | ||
import { wrapSchema } from '@graphql-tools/wrap'; | ||
import { ApolloLink, Observable } from 'apollo-link'; | ||
import gql from 'graphql-tag'; | ||
|
||
import { stitchSchemas } from '../src/stitchSchemas'; | ||
import { ExecutionResult, GraphQLError, graphql } from 'graphql'; | ||
|
||
export const typeDefs = gql` | ||
input LoginInput { | ||
username: String | ||
password: String | ||
} | ||
type LoginPayload { | ||
accessToken: String! | ||
} | ||
type Query { | ||
meh: Boolean | ||
} | ||
type Mutation { | ||
login(input: LoginInput!): LoginPayload! | ||
} | ||
`; | ||
|
||
const link = new ApolloLink(operation => { | ||
return new Observable(observer => { | ||
const responses: Record<string, ExecutionResult> = { | ||
'whatever@goodpass': { | ||
data: { | ||
login: { | ||
accessToken: 'at', | ||
}, | ||
}, | ||
}, | ||
'whatever@wrongpass': { | ||
errors: [ | ||
{ | ||
message: 'INVALID_CREDENTIALS', | ||
path: ['login', 'accessToken'], | ||
} as unknown as GraphQLError, | ||
], | ||
data: null, | ||
}, | ||
}; | ||
|
||
const response = responses[`${operation.variables.username}@${operation.variables.password}`]; | ||
if (response) { | ||
observer.next(response); | ||
observer.complete(); | ||
} else { | ||
observer.error(new Error('UNEXPECTED_ERROR')); | ||
} | ||
}); | ||
}); | ||
|
||
const authSchema = wrapSchema({ | ||
executor: linkToExecutor(link), | ||
schema: makeExecutableSchema({ typeDefs }), | ||
}); | ||
|
||
export const stitchedSchema = stitchSchemas({ | ||
subschemas: [{ schema: authSchema }], | ||
}); | ||
|
||
describe('Repro for issue #1571', () => { | ||
it('should return a successful response', async () => { | ||
const stitchedResult = await graphql( | ||
stitchedSchema, | ||
` | ||
mutation Login($username: String!, $password: String!) { | ||
login(input: { username: $username, password: $password }) { | ||
accessToken | ||
} | ||
} | ||
`, | ||
undefined, | ||
undefined, | ||
{ | ||
username: 'whatever', | ||
password: 'goodpass', | ||
}, | ||
); | ||
|
||
expect(stitchedResult).toEqual({ | ||
data: { | ||
login: { | ||
accessToken: 'at', | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should return an error response', async () => { | ||
const stitchedResult = await graphql( | ||
stitchedSchema, | ||
` | ||
mutation Login($username: String!, $password: String!) { | ||
login(input: { username: $username, password: $password }) { | ||
accessToken | ||
} | ||
} | ||
`, | ||
undefined, | ||
undefined, | ||
{ | ||
username: 'whatever', | ||
password: 'wrongpass', | ||
}, | ||
); | ||
|
||
expect(stitchedResult).toMatchObject({ | ||
errors: [ | ||
{ | ||
message: 'INVALID_CREDENTIALS', | ||
path: ['login', 'accessToken'], | ||
}, | ||
], | ||
data: null, | ||
}); | ||
}); | ||
}); |