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

Switch to [email protected] #373

Merged
merged 2 commits into from
Dec 17, 2021
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
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"build": "node resources/build.js"
},
"peerDependencies": {
"graphql": "^15.5.3"
"graphql": "^16.2.0"
},
"devDependencies": {
"@babel/core": "7.15.5",
Expand All @@ -61,7 +61,7 @@
"eslint-plugin-istanbul": "0.1.2",
"eslint-plugin-node": "11.1.0",
"flow-bin": "0.159.0",
"graphql": "15.8.0",
"graphql": "16.2.0",
"mocha": "9.1.1",
"nyc": "15.1.0",
"prettier": "2.4.0",
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/starWarsSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const { nodeInterface, nodeField } = nodeDefinitions(
return getShip(id);
}
},
(obj) => (obj.ships ? factionType : shipType),
(obj) => (obj.ships ? factionType.name : shipType.name),
);

/**
Expand Down
5 changes: 1 addition & 4 deletions src/connection/__tests__/connection-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ const userType = new GraphQLObjectType({

const { connectionType: friendConnection } = connectionDefinitions({
name: 'Friend',
// @ts-expect-error
nodeType: new GraphQLNonNull(userType),
resolveNode: (edge) => allUsers[edge.node],
edgeFields: () => ({
Expand All @@ -73,7 +72,6 @@ const { connectionType: friendConnection } = connectionDefinitions({
});

const { connectionType: userConnection } = connectionDefinitions({
// @ts-expect-error
nodeType: new GraphQLNonNull(userType),
resolveNode: (edge) => allUsers[edge.node],
});
Expand Down Expand Up @@ -184,8 +182,7 @@ describe('connectionDefinition()', () => {
});

it('generates correct types', () => {
// FIXME remove trimEnd after we update to `[email protected]`
expect(printSchema(schema).trimEnd()).to.deep.equal(dedent`
expect(printSchema(schema)).to.deep.equal(dedent`
type Query {
user: User
}
Expand Down
20 changes: 7 additions & 13 deletions src/connection/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import {
GraphQLString,
GraphQLBoolean,
getNamedType,
resolveObjMapThunk,
} from 'graphql';

import type {
GraphQLNamedOutputType,
GraphQLFieldConfigArgumentMap,
GraphQLFieldConfigMap,
GraphQLFieldConfig,
GraphQLFieldResolver,
Thunk,
ThunkObjMap,
} from 'graphql';

/**
Expand Down Expand Up @@ -79,22 +80,15 @@ export interface ConnectionConfig {
nodeType: GraphQLNamedOutputType | GraphQLNonNull<GraphQLNamedOutputType>;
resolveNode?: GraphQLFieldResolver<any, any>;
resolveCursor?: GraphQLFieldResolver<any, any>;
edgeFields?: Thunk<GraphQLFieldConfigMap<any, any>>;
connectionFields?: Thunk<GraphQLFieldConfigMap<any, any>>;
edgeFields?: ThunkObjMap<GraphQLFieldConfig<any, any>>;
connectionFields?: ThunkObjMap<GraphQLFieldConfig<any, any>>;
}

export interface GraphQLConnectionDefinitions {
edgeType: GraphQLObjectType;
connectionType: GraphQLObjectType;
}

function resolveMaybeThunk<T>(thingOrThunk: Thunk<T>): T {
return typeof thingOrThunk === 'function'
? // @ts-expect-error - if it's a function, we assume a thunk without arguments
thingOrThunk()
: thingOrThunk;
}

/**
* Returns a GraphQLObjectType for a connection with the given name,
* and whose nodes are of the specified type.
Expand All @@ -118,7 +112,7 @@ export function connectionDefinitions(
resolve: config.resolveCursor,
description: 'A cursor for use in pagination',
},
...resolveMaybeThunk(config.edgeFields ?? {}),
...resolveObjMapThunk(config.edgeFields ?? {}),
}),
});

Expand All @@ -134,7 +128,7 @@ export function connectionDefinitions(
type: new GraphQLList(edgeType),
description: 'A list of edges.',
},
...resolveMaybeThunk(config.connectionFields ?? {}),
...resolveObjMapThunk(config.connectionFields ?? {}),
}),
});

Expand Down
29 changes: 11 additions & 18 deletions src/mutation/__tests__/mutation-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
GraphQLSchema,
graphql,
graphqlSync,
printType,
printSchema,
} from 'graphql';

Expand Down Expand Up @@ -48,24 +49,17 @@ describe('mutationWithClientMutationId()', () => {
},
mutateAndGetPayload: dummyResolve,
});
const schema = wrapInSchema({ someMutation });
const source = `
mutation {
someMutation {
result
}
}
`;

expect(graphqlSync({ schema, source })).to.deep.equal({
errors: [
{
message:
'Field "someMutation" argument "input" of type "SomeMutationInput!" is required, but it was not provided.',
locations: [{ line: 3, column: 9 }],
},
],
const wrapperType = new GraphQLObjectType({
name: 'WrapperType',
fields: { someMutation },
});

expect(printType(wrapperType)).to.deep.equal(dedent`
type WrapperType {
someMutation(input: SomeMutationInput!): SomeMutationPayload
}
`);
});

it('returns the same client mutation ID', () => {
Expand Down Expand Up @@ -317,8 +311,7 @@ describe('mutationWithClientMutationId()', () => {

const schema = wrapInSchema({ someMutation });

// FIXME remove trimEnd after we update to `[email protected]`
expect(printSchema(schema).trimEnd()).to.deep.equal(dedent`
expect(printSchema(schema)).to.deep.equal(dedent`
type Query {
dummy: Int
}
Expand Down
21 changes: 7 additions & 14 deletions src/mutation/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,19 @@ import {
GraphQLNonNull,
GraphQLObjectType,
GraphQLString,
resolveObjMapThunk,
} from 'graphql';

import type {
GraphQLFieldConfig,
GraphQLFieldExtensions,
GraphQLInputFieldConfigMap,
GraphQLFieldConfigMap,
GraphQLInputFieldConfig,
GraphQLResolveInfo,
Thunk,
ThunkObjMap,
} from 'graphql';

type MutationFn = (object: any, ctx: any, info: GraphQLResolveInfo) => unknown;

function resolveMaybeThunk<T>(thingOrThunk: Thunk<T>): T {
return typeof thingOrThunk === 'function'
? // @ts-expect-error - if it's a function, we assume a thunk without arguments
thingOrThunk()
: thingOrThunk;
}

/**
* A description of a mutation consumable by mutationWithClientMutationId
* to create a GraphQLFieldConfig for that mutation.
Expand All @@ -42,8 +35,8 @@ interface MutationConfig {
description?: string;
deprecationReason?: string;
extensions?: GraphQLFieldExtensions<any, any>;
inputFields: Thunk<GraphQLInputFieldConfigMap>;
outputFields: Thunk<GraphQLFieldConfigMap<any, any>>;
inputFields: ThunkObjMap<GraphQLInputFieldConfig>;
outputFields: ThunkObjMap<GraphQLFieldConfig<any, any>>;
mutateAndGetPayload: MutationFn;
}

Expand All @@ -56,13 +49,13 @@ export function mutationWithClientMutationId(
): GraphQLFieldConfig<unknown, unknown> {
const { name, inputFields, outputFields, mutateAndGetPayload } = config;
const augmentedInputFields = () => ({
...resolveMaybeThunk(inputFields),
...resolveObjMapThunk(inputFields),
clientMutationId: {
type: GraphQLString,
},
});
const augmentedOutputFields = () => ({
...resolveMaybeThunk(outputFields),
...resolveObjMapThunk(outputFields),
clientMutationId: {
type: GraphQLString,
},
Expand Down
6 changes: 3 additions & 3 deletions src/node/__tests__/global-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ const { nodeField, nodeInterface } = nodeDefinitions(
},
(obj) => {
if (obj.name) {
return userType;
return userType.name;
}
if (obj.photoId) {
return photoType;
return photoType.name;
}

// istanbul ignore else (Can't be reached)
if (obj.text) {
return postType;
return postType.name;
}
},
);
Expand Down
7 changes: 3 additions & 4 deletions src/node/__tests__/node-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ const { nodeField, nodesField, nodeInterface } = nodeDefinitions(
},
(obj) => {
if (userData.includes(obj)) {
return userType;
return userType.name;
}
// istanbul ignore else (Can't be reached)
if (photoData.includes(obj)) {
return photoType;
return photoType.name;
}
},
);
Expand Down Expand Up @@ -315,8 +315,7 @@ describe('Node interface and fields', () => {
});

it('generates correct types', () => {
// FIXME remove trimEnd after we update to `[email protected]`
expect(printSchema(schema).trimEnd()).to.deep.equal(dedent`
expect(printSchema(schema)).to.deep.equal(dedent`
"""An object with an ID"""
interface Node {
"""The id of the object."""
Expand Down
2 changes: 1 addition & 1 deletion src/node/__tests__/nodeAsync-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const userData = [

const { nodeField, nodeInterface } = nodeDefinitions(
(id) => userData.find((obj) => obj.id === id),
() => userType,
() => userType.name,
);

const userType: GraphQLObjectType = new GraphQLObjectType({
Expand Down
3 changes: 1 addition & 2 deletions src/node/__tests__/plural-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ describe('pluralIdentifyingRootField()', () => {
});

it('generates correct types', () => {
// FIXME remove trimEnd after we update to `[email protected]`
expect(printSchema(schema).trimEnd()).to.deep.equal(dedent`
expect(printSchema(schema)).to.deep.equal(dedent`
type Query {
"""Map from a username to the user"""
usernames(usernames: [String!]!): [User]
Expand Down