New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
schema import types (or GraphQLSchema) from typescript #9932
Comments
Hello 👋 I've got a few questions about your setup:
If you still see errors, please help provide specific expectation and errors you are seeing. 🙂 |
Because this project is moving an existing api into a codegen one; I don't necessarily have the freedom to change it to meet needs. With graphql, changing types breaks signatures and clients error out, it's really brittle with respect to type values. I'm trying both techniques to achieve the goal: typescript enum type definition as well as enumValues codegen config. The typescript stuff breaks easy, so that's this ticket at a minimum. The enumValues works in an empty project, but stops working in a nontrival context or when your server preset is used. |
Thanks for the details, I think I understand now that you want to migrate enum in TypeScript to use SDL. If that's the case, here's the steps I can recommend right now:
type Query {
colors: [Color]
user(id: String): User
}
type User {
id: String
name: String
language: ISOLanguage
}
enum ISOLanguage {
EN
ES
RU
}
enum Color {
RED
GREEN
BLUE
}
// src/schema/base/resolvers/User.ts
import type { UserResolvers } from "./../../types.generated";
export const User: UserResolvers = {
/* Implement User resolver logic here */
language: () => {
return "EN"; // Can also be "ES", "RU"
},
}; |
Your example does not appear to be type compatible with the codebase. We're not talking about typescript, we're talking about graphql. I have an existing codebase, it's SDL is a megabyte already. I cannot generate types compatible with the existing types. The existing type is:
The reason it's incompatible is the internal representation is a mismatch between what you're doing, and what's needed for compatibility. Please review my links again:
|
I found a codegen that "works", in that it accepts the parameters I'm passing
without server presets the codegen spits:
which compiles to javascript:
with the server preset and the enumValues it spits
compiling both these down to javascript with tsc:
I'm blocked until I can get the server preset to generate the code I'm looking for which transforms SDL uppercase to database lowercase. |
typescript defined types (re: issue title) also seem to work when not defineConfig |
If you need to use TypeScript enum, you can set defineConfig({
typesPluginsConfig: {
enumsAsTypes: false,
enumValues: {
ISOLanguage: {
EN: "en",
ES: "es",
RU: "ru",
},
},
},
}) |
Yup, I figured out that part; and changed the codegen repo to purely represent the fact that typescript schema don't work at all with server preset. In particular it early outs on
and so my existing typescript is not useful and has to be ported to SDL to work with the server config. I'm unblocked now, so that's good; but only allowing SDL schema definition should be documented, as I lost much time trying to get it to work. (And it's fine w/o the config preset in the way) |
Ah, gotcha, this is something I wasn't aware of. Thanks! I can look into improving the preset by adding this support. Do you have other questions? Otherwise we can close this issue 🙂 |
no other questions, this issue is represented by "adding typescript support to preset". If that's tracked elsewhere, this is now done. |
Thank you @MichaelRando , I have created an issue in Server Preset repo: eddeee888/graphql-code-generator-plugins#264 I'll look into adding support for your request in the future. |
Which packages are impacted by your issue?
@graphql-codegen/cli, @graphql-codegen/core, @graphql-codegen/schema-ast, @graphql-codegen/typescript-operations, @graphql-codegen/typescript-resolvers, @graphql-codegen/typescript
Describe the bug
My failing example at https://github.com/MichaelRando/graphql-code-generator-test
I want to load schema from graphql. I want to customize enum with internal string substitution, otherwise I'm blocked on porting this api to codegen. I've followed the documentation to best my ability.
Your Example Website or App
https://github.com/MichaelRando/graphql-code-generator-test
Steps to Reproduce the Bug or Issue
sync the repo and try to generate via
yarn codegen
Expected behavior
output files and internal representation enum are expected.
Screenshots or Videos
No response
Platform
graphql
version: 16.8@graphql-codegen/*
version(s):5.0.2Codegen Config File
import type { CodegenConfig } from "@graphql-codegen/cli";
import { defineConfig } from "@eddeee888/gcg-typescript-resolver-files";
const config: CodegenConfig = {
schema: ["./moduleFile.ts", "./schemaFile.graphql"],
require: ["ts-node/register"],
generates: {
schema: {
...defineConfig(),
config: {
enumValues: {
Color: {
RED: "red",
GREEN: "green",
BLUE: "blue",
},
},
},
},
},
};
export default config;
Additional context
import {
GraphQLEnumType,
GraphQLObjectType,
GraphQLSchema,
GraphQLString,
} from "graphql";
const languageType = new GraphQLEnumType({
name: "ISOLanguage",
values: {
EN: {
value: "en",
},
ES: {
value: "es",
},
RU: {
value: "ru",
},
},
});
const userType = new GraphQLObjectType({
name: "User",
fields: {
id: { type: GraphQLString },
name: { type: GraphQLString },
language: { type: languageType },
},
});
// Define the Query type
const queryType = new GraphQLObjectType({
name: "Query",
fields: {
user: {
type: userType,
//
args
describes the arguments that theuser
query acceptsargs: {
id: { type: GraphQLString },
},
resolve: (_, { id }) => {
return id;
},
},
},
});
const schema = new GraphQLSchema({
query: queryType,
});
export { schema };
export default schema;
The text was updated successfully, but these errors were encountered: