Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@
"supertest": "4.0.2"
},
"peerDependencies": {
"graphql": "^0.12.0 || ^0.13.0 || ^14.0.0"
"graphql": "^14.2.0"
}
}
42 changes: 42 additions & 0 deletions src/__tests__/http-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,48 @@ describe('test harness', () => {
});
});

it('Allows passing in a typeResolver', async () => {
const schema = buildSchema(`
type Foo {
foo: String
}

type Bar {
bar: String
}

union UnionType = Foo | Bar

type Query {
test: UnionType
}
`);
const app = server();

get(
app,
urlString(),
graphqlHTTP({
schema,
rootValue: { test: {} },
typeResolver: () => 'Bar',
}),
);

const response = await request(app).get(
urlString({
query: '{ test { __typename } }',
}),
);

expect(response.status).to.equal(200);
expect(JSON.parse(response.text)).to.deep.equal({
data: {
test: { __typename: 'Bar' },
},
});
});

it('Uses request as context by default', async () => {
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
Expand Down
13 changes: 11 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ import {
getOperationAST,
specifiedRules,
} from 'graphql';
import type { ExecutionArgs, ExecutionResult } from 'graphql';
import httpError from 'http-errors';
import url from 'url';

import { parseBody } from './parseBody';
import { renderGraphiQL } from './renderGraphiQL';

import type {
ExecutionArgs,
ExecutionResult,
DocumentNode,
GraphQLError,
GraphQLSchema,
GraphQLFieldResolver,
GraphQLTypeResolver,
ValidationContext,
ASTVisitor,
} from 'graphql';
Expand Down Expand Up @@ -139,6 +139,13 @@ export type OptionsData = {
* value or method on the source value with the field's name).
*/
fieldResolver?: ?GraphQLFieldResolver<any, any>,

/**
* A type resolver function to use when none is provided by the schema.
* If not provided, the default type resolver is used (which looks for a
* `__typename` field or alternatively calls the `isTypeOf` method).
*/
typeResolver?: ?GraphQLTypeResolver<any, any>,
};

/**
Expand Down Expand Up @@ -234,6 +241,7 @@ function graphqlHTTP(options: Options): Middleware {
const schema = optionsData.schema;
const rootValue = optionsData.rootValue;
const fieldResolver = optionsData.fieldResolver;
const typeResolver = optionsData.typeResolver;
const graphiql = optionsData.graphiql;
context = optionsData.context || request;

Expand Down Expand Up @@ -327,6 +335,7 @@ function graphqlHTTP(options: Options): Middleware {
variableValues: variables,
operationName,
fieldResolver,
typeResolver,
});
} catch (contextError) {
// Return 400: Bad Request if any execution context errors exist.
Expand Down