Skip to content

Commit

Permalink
Fix fields incorrectly being optional
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown committed Apr 26, 2022
1 parent 58cf5de commit 5b800e7
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-experts-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ts-gql/compiler": patch
---

Fixed nullable fields being optional in the generated types
8 changes: 8 additions & 0 deletions .changeset/strange-readers-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@ts-gql/apollo": patch
"@ts-gql/eslint-plugin": patch
"@ts-gql/fetch": patch
"@ts-gql/next": patch
---

Republish after broken release
6 changes: 6 additions & 0 deletions packages/compiler/src/operation-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ async function generateOperationTypes(
namespacedImportName: "SchemaTypes",
immutableTypes: config.readonlyTypes,
noExport: true,
avoidOptionals: {
field: true,
inputValue: false,
object: false,
defaultValue: false,
},
nonOptionalTypename: config.addTypename,
skipTypename: !config.addTypename,
namingConvention: "keep",
Expand Down
6 changes: 6 additions & 0 deletions packages/compiler/src/schema-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ function generateSchemaTypes(
immutableTypes: config.readonlyTypes,
nonOptionalTypename: true,
namingConvention: "keep",
avoidOptionals: {
field: true,
inputValue: false,
object: false,
defaultValue: false,
},
},
},
],
Expand Down
8 changes: 4 additions & 4 deletions packages/compiler/src/test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ Object {
"errors": Array [],
"fsOperations": Array [
Object {
"content": "// ts-gql-integrity:d63b0d76f8941e6fa31e36de1ef6f553
"content": "// ts-gql-integrity:8863c8f285259624d86cf9ea58c8e3cf
/*
ts-gql-meta-begin
{
Expand All @@ -184,7 +184,7 @@ import * as SchemaTypes from \\"./@schema\\";
import { TypedDocumentNode } from \\"@ts-gql/tag\\";
type ThingQueryVariables = SchemaTypes.Exact<{
optional?: SchemaTypes.InputMaybe<SchemaTypes.Scalars['String']>;
optional: SchemaTypes.InputMaybe<SchemaTypes.Scalars['String']>;
required: SchemaTypes.Scalars['String'];
}>;
Expand Down Expand Up @@ -221,7 +221,7 @@ Object {
"errors": Array [],
"fsOperations": Array [
Object {
"content": "// ts-gql-integrity:7830dea677fdaea0679792ddce6ca199
"content": "// ts-gql-integrity:aef83417457632256cd8091ba15be34e
/*
ts-gql-meta-begin
{
Expand All @@ -234,7 +234,7 @@ import * as SchemaTypes from \\"./@schema\\";
import { TypedDocumentNode } from \\"@ts-gql/tag\\";
type ThingQueryVariables = SchemaTypes.Exact<{
optional?: SchemaTypes.InputMaybe<SchemaTypes.Scalars['String']>;
optional: SchemaTypes.InputMaybe<SchemaTypes.Scalars['String']>;
}>;
Expand Down
88 changes: 75 additions & 13 deletions packages/compiler/src/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function setupEnv(specificSchema: string = schema) {
JSON.stringify(
{
name: "something",
"ts-gql": { schema: "schema.graphql" },
"ts-gql": { schema: "schema.graphql" }
},
null,
2
Expand All @@ -38,13 +38,13 @@ async function setupEnv(specificSchema: string = schema) {
async function build(cwd: string) {
let result = await getGeneratedTypes(await getConfig(cwd), true);
return {
errors: result.errors.map((x) =>
errors: result.errors.map(x =>
stripAnsi(x.replace(slash(cwd), "CURRENT_WORKING_DIRECTORY"))
),
fsOperations: result.fsOperations
.filter((x) => !path.parse(x.filename).name.startsWith("@"))
.filter(x => !path.parse(x.filename).name.startsWith("@"))
.sort((a, b) => a.filename.localeCompare(b.filename))
.map((x) => ({ ...x, filename: slash(path.relative(cwd, x.filename)) })),
.map(x => ({ ...x, filename: slash(path.relative(cwd, x.filename)) }))
};
}

Expand Down Expand Up @@ -79,7 +79,7 @@ test("basic", async () => {
query Thing {
hello
}
`,
`
])
);
expect(await build(dir)).toMatchSnapshot();
Expand Down Expand Up @@ -110,7 +110,7 @@ test("list with fragment works as expected", async () => {
other
}
}
`,
`
])
);

Expand Down Expand Up @@ -143,7 +143,7 @@ test("something", async () => {
...Frag_b
}
}
`,
`
])
);
expect(await build(dir)).toMatchSnapshot();
Expand Down Expand Up @@ -173,7 +173,7 @@ test("errors in fragments are not shown for usages", async () => {
i
}
}
`,
`
])
);
expect((await build(dir)).errors).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -208,7 +208,7 @@ test("with directory that ends with .ts", async () => {
query Thing {
hello
}
`,
`
])
);
const dirEndsWithTs = path.join(dir, "thing.ts");
Expand All @@ -229,7 +229,7 @@ test("optional variable", async () => {
query Thing($optional: String) {
optional(thing: $optional)
}
`,
`
])
);
const dirEndsWithTs = path.join(dir, "thing.ts");
Expand All @@ -251,7 +251,7 @@ test("optional and required variables", async () => {
optional(thing: $optional)
other: optional(thing: $required)
}
`,
`
])
);
const dirEndsWithTs = path.join(dir, "thing.ts");
Expand All @@ -272,7 +272,7 @@ test("required variable", async () => {
query Thing($required: String!) {
optional(thing: $required)
}
`,
`
])
);
const dirEndsWithTs = path.join(dir, "thing.ts");
Expand Down Expand Up @@ -309,7 +309,7 @@ test.skip("fragments with circular dependencies error well", async () => {
...Frag_b
}
}
`,
`
])
);
const dirEndsWithTs = path.join(dir, "thing.ts");
Expand All @@ -319,3 +319,65 @@ test.skip("fragments with circular dependencies error well", async () => {

expect(await build(dir)).toMatchSnapshot();
});

test("returned nullable fields are not nullable", async () => {
let dir = await setupEnv();

await fs.writeFile(
path.join(dir, "index.tsx"),
makeSourceFile([
graphql`
query Thing {
something
}
`
])
);

expect(await build(dir)).toMatchInlineSnapshot(`
Object {
"errors": Array [],
"fsOperations": Array [
Object {
"content": "// ts-gql-integrity:dfdfd721d6fff5c53f80455b9f87f5fe
/*
ts-gql-meta-begin
{
\\"hash\\": \\"ab3ae6d6a5717aa0534198074d098314\\"
}
ts-gql-meta-end
*/
import * as SchemaTypes from \\"./@schema\\";
import { TypedDocumentNode } from \\"@ts-gql/tag\\";
type ThingQueryVariables = SchemaTypes.Exact<{ [key: string]: never; }>;
type ThingQuery = { readonly __typename: 'Query', readonly something: string | null };
export type type = TypedDocumentNode<{
type: \\"query\\";
result: ThingQuery;
variables: ThingQueryVariables;
documents: SchemaTypes.TSGQLDocuments;
fragments: SchemaTypes.TSGQLRequiredFragments<\\"none\\">
}>
declare module \\"./@schema\\" {
interface TSGQLDocuments {
Thing: type;
}
}
export const document = JSON.parse(\\"{\\\\\\"kind\\\\\\":\\\\\\"Document\\\\\\",\\\\\\"definitions\\\\\\":[{\\\\\\"kind\\\\\\":\\\\\\"OperationDefinition\\\\\\",\\\\\\"operation\\\\\\":\\\\\\"query\\\\\\",\\\\\\"name\\\\\\":{\\\\\\"kind\\\\\\":\\\\\\"Name\\\\\\",\\\\\\"value\\\\\\":\\\\\\"Thing\\\\\\"},\\\\\\"variableDefinitions\\\\\\":[],\\\\\\"directives\\\\\\":[],\\\\\\"selectionSet\\\\\\":{\\\\\\"kind\\\\\\":\\\\\\"SelectionSet\\\\\\",\\\\\\"selections\\\\\\":[{\\\\\\"kind\\\\\\":\\\\\\"Field\\\\\\",\\\\\\"name\\\\\\":{\\\\\\"kind\\\\\\":\\\\\\"Name\\\\\\",\\\\\\"value\\\\\\":\\\\\\"something\\\\\\"},\\\\\\"arguments\\\\\\":[],\\\\\\"directives\\\\\\":[]}]}}]}\\")
",
"filename": "__generated__/ts-gql/Thing.ts",
"type": "output",
},
],
}
`);
});

0 comments on commit 5b800e7

Please sign in to comment.