Skip to content

Commit 536bcc1

Browse files
committed
Bugfix #41: Fix field type determination
1 parent 4f4d349 commit 536bcc1

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

src/graphql/query-builder.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,8 @@ export default class QueryBuilder {
172172
const isForeignKey = model.skipField(key);
173173
const skipFieldDueId = (key === 'id' || isForeignKey) && !allowIdFields;
174174

175-
const schema = Context.getInstance().schema!;
176-
const type = schema.getType(model.singularName + (filter ? 'Filter' : ''));
177-
const schemaField = type ? (filter ? type.inputFields! : type.fields!).find(f => f.name === key) : null;
175+
let schemaField: GraphQLField | undefined = this.findSchemaFieldForArgument(key, field, model, filter);
176+
178177
const isConnectionField = schemaField && Schema.getTypeNameOfField(schemaField).endsWith('TypeConnection');
179178

180179
// Ignore null fields, ids and connections
@@ -253,6 +252,30 @@ export default class QueryBuilder {
253252
}
254253
}
255254

255+
private static findSchemaFieldForArgument (
256+
name: String, field: GraphQLField | null, model: Model, isFilter: boolean
257+
): GraphQLField | undefined {
258+
259+
const schema = Context.getInstance().schema!;
260+
let schemaField: GraphQLField | undefined;
261+
262+
if (field) {
263+
schemaField = field.args.find(f => f.name === name);
264+
265+
if (!schemaField) {
266+
Context.getInstance().logger.warn(`Could find the argument with name ${name} for the mutation/query ${field.name}`);
267+
}
268+
} else {
269+
// We try to find the FilterType or at least the Type this query belongs to.
270+
const type = schema.getType(model.singularName + (isFilter ? 'Filter' : ''));
271+
272+
// Next we try to find the field from the type
273+
schemaField = type ? (isFilter ? type.inputFields! : type.fields!).find(f => f.name === name) : undefined;
274+
}
275+
276+
return schemaField;
277+
}
278+
256279
/**
257280
* Generates the fields for all related models.
258281
*

test/integration/VuexORMGraphQL.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,14 +447,14 @@ query UnpublishedPosts($userId: ID!, $id: ID!) {
447447
const post = Post.find(1);
448448

449449
const request = await recordGraphQLRequest(async () => {
450-
await post.$mutate({name: 'upvotePost', args: { captchaToken: '15' }});
450+
await Post.mutate({name: 'upvotePost', args: { captchaToken: '15', postId: post.id }});
451451
});
452452

453453
expect(request.variables.captchaToken).toEqual('15');
454-
expect(request.variables.id).toEqual(post.id);
454+
expect(request.variables.postId).toEqual(post.id);
455455
expect(request.query).toEqual(`
456-
mutation UpvotePost($captchaToken: String!, $id: ID!) {
457-
upvotePost(captchaToken: $captchaToken, id: $id) {
456+
mutation UpvotePost($captchaToken: String!, $postId: ID!) {
457+
upvotePost(captchaToken: $captchaToken, postId: $postId) {
458458
id
459459
content
460460
title

test/support/mock-schema.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export const typeDefs = `
5151
updateTariffOption(id: ID!, tariffOption: TariffOptionInput!): TariffOption!
5252
updateTariff(id: ID!, tariff: TariffInput!): Tariff!
5353
54-
upvotePost(captchaToken: String!, id: ID!): Post!
54+
upvotePost(captchaToken: String!, postId: ID!): Post!
5555
sendSms(to: String!, text: String!): SmsStatus!
5656
reorderItems(id: ID!, itemIds: [ID]!): PostTypeConnection
5757
}
@@ -612,7 +612,7 @@ export const resolvers = {
612612
Mutation: {
613613
// Customs
614614

615-
upvotePost: (parent, { captchaToken, id }) => findOne(Post, posts, id),
615+
upvotePost: (parent, { captchaToken, postId }) => findOne(Post, posts, postId),
616616
sendSms: (parent, { to, text }) => ({ delivered: true }),
617617

618618

0 commit comments

Comments
 (0)