Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

Commit

Permalink
Render all variables correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
timkendall committed Dec 29, 2020
1 parent 6b80f91 commit 6dc8010
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
9 changes: 6 additions & 3 deletions __tests__/starwars/starwars.api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
NamedType,
Argument,
Value,
Field,
InlineFragment,
Operation,
Expand Down Expand Up @@ -425,7 +424,9 @@ interface HumanSelector {
* @description Height in the preferred unit, default is meters
*/

height: (variables: { unit: unknown }) => Field<"height", [/* @todo */]>;
height: (variables: {
unit?: Variable<"unit"> | LengthUnit;
}) => Field<"height", [Argument<"unit", Variable<"unit"> | LengthUnit>]>;

/**
* @description Mass in kilograms, or null if unknown
Expand Down Expand Up @@ -875,7 +876,9 @@ interface StarshipSelector {
* @description Length of the starship, along the longest axis
*/

length: (variables: { unit: unknown }) => Field<"length", [/* @todo */]>;
length: (variables: {
unit?: Variable<"unit"> | LengthUnit;
}) => Field<"length", [Argument<"unit", Variable<"unit"> | LengthUnit>]>;

coordinates: () => Field<"coordinates">;
}
Expand Down
8 changes: 6 additions & 2 deletions examples/apollo-client/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { ApolloClient, InMemoryCache } from "@apollo/client";

import { Result } from "../../src";
import { query, IQuery } from "../../__tests__/starwars/starwars.api";
import {
query,
IQuery,
LengthUnit,
} from "../../__tests__/starwars/starwars.api";

const client = new ApolloClient({ cache: new InMemoryCache() });

const example = query("Apollo Example", (t) => [
t.starship({ id: "foo" }, (t) => [
t.id(),
t.name(),
t.length({ unit: "any" }),
t.length({ unit: LengthUnit.FOOT }),
t.coordinates(),
]),
]);
Expand Down
19 changes: 4 additions & 15 deletions src/Codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export class Codegen {
import {
NamedType,
Argument,
Value,
Field,
InlineFragment,
Operation,
Expand Down Expand Up @@ -455,14 +454,14 @@ export class Codegen {
? toPrimitive(baseType)
: baseType.name;

// @todo render arguments correctly
return args.length > 0
? `${jsDocComment}\n${name}: (variables: { ${args
.map((a) => `${a.name}: unknown`)
.join(", ")} }) => Field<"${name}", [/* @todo */]>`
.map(renderVariable)
.join(", ")} }) => Field<"${name}", [ ${args
.map(renderArgument)
.join(", ")} ]>`
: `${jsDocComment}\n${name}: () => Field<"${name}">`;
} else {
// @todo render arguments correctly
// @todo restrict allowed Field types
return args.length > 0
? `
Expand All @@ -486,10 +485,6 @@ export class Codegen {
private field(field: GraphQLField<any, any, any>): string {
const { name, args, type } = field;

const isList =
type instanceof GraphQLList ||
(type instanceof GraphQLNonNull && type.ofType instanceof GraphQLList);
const isNonNull = type instanceof GraphQLNonNull;
const baseType = getBaseOutputType(type);

const jsDocComments = [
Expand All @@ -510,11 +505,6 @@ export class Codegen {
baseType instanceof GraphQLScalarType ||
baseType instanceof GraphQLEnumType
) {
const fieldType =
baseType instanceof GraphQLScalarType
? toPrimitive(baseType)
: baseType.name;

// @todo render arguments correctly
return args.length > 0
? jsDocComment.concat(`${name}: (variables) => new Field("${name}"),`)
Expand All @@ -529,7 +519,6 @@ export class Codegen {
: `new Argument("${arg.name}", variables.${arg.name})`;
};

// @todo render arguments correctly
// @todo restrict allowed Field types
return args.length > 0
? `
Expand Down

0 comments on commit 6dc8010

Please sign in to comment.