Skip to content
Open
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
26 changes: 26 additions & 0 deletions examples/graphql-yoga/codegen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { CodegenConfig } from "@graphql-codegen/cli";
import { schema } from "./src/schema";

const config: CodegenConfig = {
schema,
emitLegacyCommonJSImports: false,
generates: {
"./src/modules/": {
preset: "graphql-modules",
presetConfig: {
baseTypesPath: "../__generated__/graphql.ts",
filename: "__generated__/types.ts",
},
plugins: [
{ add: { content: "/* eslint-disable */\n// @ts-nocheck" } },
"typescript",
"typescript-resolvers",
],
},
"./__generated__/schema.graphql": {
plugins: ["schema-ast"],
},
},
};

export default config;
13 changes: 11 additions & 2 deletions examples/graphql-yoga/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"packageManager": "yarn@3.6.3",
"scripts": {
"start": "node --loader tsx ./index.ts",
"preinstall": "cd ../../ && yarn pack"
"preinstall": "cd ../../ && yarn pack",
"generate:types": "graphql-codegen-esm"
},
"volta": {
"extends": "../../package.json"
Expand All @@ -14,14 +15,22 @@
"@envelop/core": "^4.0.3",
"@envelop/dataloader": "^5.0.3",
"@envelop/graphql-modules": "^5.0.3",
"@frontside/hydraphql": "^0.0.1",
"@frontside/hydraphql": "^0.1.0",
"@graphql-tools/load-files": "^7.0.0",
"@graphql-tools/utils": "^10.0.0",
"dataloader": "^2.2.2",
"graphql": "^16.8.1",
"graphql-modules": "^2.2.0",
"graphql-yoga": "^4.0.4"
},
"devDependencies": {
"@graphql-codegen/add": "^5.0.0",
"@graphql-codegen/cli": "^4.0.0",
"@graphql-codegen/graphql-modules-preset": "^4.0.0",
"@graphql-codegen/plugin-helpers": "^5.0.0",
"@graphql-codegen/schema-ast": "^4.0.0",
"@graphql-codegen/typescript": "^4.0.0",
"@graphql-codegen/typescript-resolvers": "^4.0.0",
"tsx": "^3.13.0"
}
}
33 changes: 8 additions & 25 deletions examples/graphql-yoga/src/modules/starwars/starwars.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { loadFilesSync } from "@graphql-tools/load-files";
import { createModule } from "graphql-modules";
import { encodeId } from "@frontside/hydraphql";
import { Resolvers } from "../../__generated__/graphql";

const STARWARS_API = "https://swapi.dev/api";

Expand All @@ -12,10 +13,7 @@ export const StarWars = createModule({
),
resolvers: {
Query: {
people: async (
_: unknown,
{ search }: { search: string },
): Promise<{ id: string }[]> => {
people: async (_: unknown, { search }): Promise<{ id: string }[]> => {
const response = await fetch(
`${STARWARS_API}/people/?search=${search}`,
);
Expand All @@ -30,10 +28,7 @@ export const StarWars = createModule({
}),
}));
},
films: async (
_: unknown,
{ search }: { search: string },
): Promise<{ id: string }[]> => {
films: async (_: unknown, { search }): Promise<{ id: string }[]> => {
const response = await fetch(`${STARWARS_API}/films/?search=${search}`);
const films = (
(await response.json()) as { results: { url: string }[] }
Expand All @@ -46,10 +41,7 @@ export const StarWars = createModule({
}),
}));
},
starships: async (
_: unknown,
{ search }: { search: string },
): Promise<{ id: string }[]> => {
starships: async (_: unknown, { search }): Promise<{ id: string }[]> => {
const response = await fetch(
`${STARWARS_API}/starships/?search=${search}`,
);
Expand All @@ -64,10 +56,7 @@ export const StarWars = createModule({
}),
}));
},
vehicles: async (
_: unknown,
{ search }: { search: string },
): Promise<{ id: string }[]> => {
vehicles: async (_: unknown, { search }): Promise<{ id: string }[]> => {
const response = await fetch(
`${STARWARS_API}/vehicles/?search=${search}`,
);
Expand All @@ -82,10 +71,7 @@ export const StarWars = createModule({
}),
}));
},
species: async (
_: unknown,
{ search }: { search: string },
): Promise<{ id: string }[]> => {
species: async (_: unknown, { search }): Promise<{ id: string }[]> => {
const response = await fetch(
`${STARWARS_API}/species/?search=${search}`,
);
Expand All @@ -100,10 +86,7 @@ export const StarWars = createModule({
}),
}));
},
planets: async (
_: unknown,
{ search }: { search: string },
): Promise<{ id: string }[]> => {
planets: async (_: unknown, { search }): Promise<{ id: string }[]> => {
const response = await fetch(
`${STARWARS_API}/planets/?search=${search}`,
);
Expand All @@ -119,5 +102,5 @@ export const StarWars = createModule({
}));
},
},
},
} as Resolvers,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cowboyd @wKich This current setup just makes typescript not complain but it doesn't help people writing the resolvers. Is there a better way to type this so we can actually use TypeScript for guidance in parameters?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you are right. It's better to declare resolvers:

const resolvers: Resolvers = { /* ... */ }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option is to wrap createModule() to require the type of resolvers to be Resolvers. Another pattern I've become fond of is to use a function:

function moduleResolvers(): Resolvers {
}

and then invoke it like:

createModule({
  id: "starwars",
  dirname: import.meta.url,
  resolvers: moduleResolvers(),
});

Or as something that can be spread in:

function moduleResolvers(): { resolvers: Resolvers } {
 //....
}
createModule({
  id: "starwars",
  dirname: import.meta.url,
  ...moduleResolvers(),
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with the ...moduleResolvers() or moduleResolves() - both work fine.

});
5 changes: 5 additions & 0 deletions examples/graphql-yoga/src/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { printSchemaWithDirectives } from "@graphql-tools/utils";
import { transformSchema } from "@frontside/hydraphql";
import { modules } from "./modules";

export const schema = printSchemaWithDirectives(transformSchema(modules));
Loading