From 5977e8c9913c5fd2a5f18953952990e39eb47d10 Mon Sep 17 00:00:00 2001 From: calebmer Date: Sat, 8 Oct 2016 16:44:49 -0400 Subject: [PATCH] feat(postgraphql): add related fields to mutation top level for Relay 1 --- .../postgraphqlIntegrationOperations-test.js.snap | 4 ++++ .../postgraphqlIntegrationSchema-test.js.snap | 6 ++++++ .../fixtures/queries/procedure-mutation.graphql | 2 +- .../createPGProcedureMutationGQLFieldEntries.ts | 14 ++++++++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/postgraphql/__tests__/__snapshots__/postgraphqlIntegrationOperations-test.js.snap b/src/postgraphql/__tests__/__snapshots__/postgraphqlIntegrationOperations-test.js.snap index a141c721b0..fd32a05d19 100644 --- a/src/postgraphql/__tests__/__snapshots__/postgraphqlIntegrationOperations-test.js.snap +++ b/src/postgraphql/__tests__/__snapshots__/postgraphqlIntegrationOperations-test.js.snap @@ -1371,6 +1371,10 @@ Object { "integer": 2, }, "tableMutation": Object { + "personByAuthorId": Object { + "id": 5, + "name": "Johnny Tucker", + }, "post": Object { "__id": "WyJwb3N0cyIsNV0=", "authorId": 5, diff --git a/src/postgraphql/__tests__/__snapshots__/postgraphqlIntegrationSchema-test.js.snap b/src/postgraphql/__tests__/__snapshots__/postgraphqlIntegrationSchema-test.js.snap index 24a961627d..86d87770f7 100644 --- a/src/postgraphql/__tests__/__snapshots__/postgraphqlIntegrationSchema-test.js.snap +++ b/src/postgraphql/__tests__/__snapshots__/postgraphqlIntegrationSchema-test.js.snap @@ -1257,6 +1257,9 @@ input TableMutationInput { type TableMutationPayload { clientMutationId: String post: Post + + # Reads a single \`Person\` that is related to this \`Post\`. + personByAuthorId: Person query: Query } @@ -2874,6 +2877,9 @@ input TableMutationInput { type TableMutationPayload { clientMutationId: String post: Post + + # Reads a single \`Person\` that is related to this \`Post\`. + personByAuthorId: Person query: Query } diff --git a/src/postgraphql/__tests__/fixtures/queries/procedure-mutation.graphql b/src/postgraphql/__tests__/fixtures/queries/procedure-mutation.graphql index ae786e0f64..bc566c4e79 100644 --- a/src/postgraphql/__tests__/fixtures/queries/procedure-mutation.graphql +++ b/src/postgraphql/__tests__/fixtures/queries/procedure-mutation.graphql @@ -9,7 +9,7 @@ mutation { mult4(input: { arg0: 5, arg1: 2 }) { integer } typesMutation(input: { a: 50, b: false, c: "xyz", d: [1, 2, 3], e: "{\"a\":1,\"b\":2,\"c\":3}", f: { start: { value: 1, inclusive: false }, end: { value: 5, inclusive: false } } }) { boolean } compoundTypeMutation(input: { object: { a: 419, b: "easy cheesy baked potatoes", c: RED, fooBar: 8 } }) { compoundType { a b c d fooBar } } - tableMutation(input: { id: 5 }) { post { __id id headline authorId } } + tableMutation(input: { id: 5 }) { post { __id id headline authorId } personByAuthorId { id name } } tableSetMutation(input: {}) { people { name } } intSetMutation(input: { x: 5, z: 6 }) { integers } noArgsMutation(input: { clientMutationId: "x" }) { clientMutationId integer } diff --git a/src/postgraphql/schema/procedures/hooks/createPGProcedureMutationGQLFieldEntries.ts b/src/postgraphql/schema/procedures/hooks/createPGProcedureMutationGQLFieldEntries.ts index b17a2385d0..396baf06ee 100644 --- a/src/postgraphql/schema/procedures/hooks/createPGProcedureMutationGQLFieldEntries.ts +++ b/src/postgraphql/schema/procedures/hooks/createPGProcedureMutationGQLFieldEntries.ts @@ -11,8 +11,10 @@ import { formatName } from '../../../../graphql/utils' import BuildToken from '../../../../graphql/schema/BuildToken' import createMutationGQLField from '../../../../graphql/schema/createMutationGQLField' import transformGQLInputValue from '../../../../graphql/schema/transformGQLInputValue' +import createCollectionRelationTailGQLFieldEntries from '../../../../graphql/schema/collection/createCollectionRelationTailGQLFieldEntries' import { sql } from '../../../../postgres/utils' import { PGCatalog, PGCatalogProcedure } from '../../../../postgres/introspection' +import PGCollection from '../../../../postgres/inventory/collection/PGCollection' import pgClientFromContext from '../../../../postgres/inventory/pgClientFromContext' import transformPGValueIntoValue from '../../../../postgres/inventory/transformPGValueIntoValue' import createPGProcedureFixtures from '../createPGProcedureFixtures' @@ -42,8 +44,16 @@ function createPGProcedureMutationGQLFieldEntry ( pgCatalog: PGCatalog, pgProcedure: PGCatalogProcedure, ): [string, GraphQLFieldConfig] { + const { inventory } = buildToken const fixtures = createPGProcedureFixtures(buildToken, pgCatalog, pgProcedure) + // See if the output type of this procedure is a single object, try to find a + // `PGCollection` which has the same type. If it exists we add some extra + // stuffs. + const pgCollection = !pgProcedure.returnsSet + ? inventory.getCollections().find(collection => collection instanceof PGCollection && collection._pgClass.typeId === fixtures.return.pgType.id) + : null + // Create our GraphQL input fields users will use to input data into our // procedure. const inputFields = fixtures.args.map<[string, GraphQLInputFieldConfig]>( @@ -75,6 +85,10 @@ function createPGProcedureMutationGQLFieldEntry ( resolve: value => value, }], + + // Add related objects if there is an associated `PGCollection`. This + // helps in Relay 1. + ...(pgCollection ? createCollectionRelationTailGQLFieldEntries(buildToken, pgCollection) : []), ], // Actually execute the procedure here.