标签: graphql-codegen

使用 Hasura graphql 模式时如何自定义 graphql-code-generator 生成的字段的类型

我想覆盖 Hasura 生成的 graphql 模式中特定字段的 jsonb 类型,并通过 graphql-code-generator 运行。

\n\n

我有一个customListjsonb 类型的字段。Ths 用于包含 json 对象数组。当将 graphql-code-generator 与 TypeScript 插件一起使用时,生成的类型解析为any. 我试图找出如何仅使用该特定字段的自定义类型来覆盖它。

\n\n

下面的代码片段显示了 graphql 模式的相关部分,以及目标 graphql 类型覆盖。到目前为止,我尝试过的所有操作都会导致代码生成错误

\n\n

图Ql模式

\n\n
  //schema.json  \n  ...\n  {\n    "kind": "OBJECT",\n    "name": \xe2\x80\x9cMyEntity\xe2\x80\x9d,\n    "description": "columns and relationships of MyEntity",\n    "fields": [\n        ...\n        {\n        "name": "customList",\n        "description": "",\n        "args": [\n            {\n            "name": "path",\n            "description": "JSON select path",\n            "type": {\n                "kind": "SCALAR",\n                "name": "String",\n                "ofType": null\n            },\n            "defaultValue": null\n            }\n        ],\n        "type": {\n            "kind": …
Run Code Online (Sandbox Code Playgroud)

typescript graphql hasura graphql-codegen

6
推荐指数
1
解决办法
2774
查看次数

GraphQL 代码生成器 - 无法从 Nestjs 服务器端点加载我的 GQL 架构

我在 Angular 应用程序上使用graphQL 代码生成器,并尝试从本地 Nestjs 应用程序加载我的架构。

这是 codegen.yml 文件:

schema: http://localhost:3000/graphql
documents: ./src/app/graphql/**/*.graphql
generates:
  ./src/app/graphql/types.ts:
    plugins:
      - typescript
      - typescript-operations
      - typescript-apollo-angular
Run Code Online (Sandbox Code Playgroud)

当我运行 graphql-codegen 时,出现以下错误:

 Failed to load schema from http://localhost:3000/graphql:

        invalid json response body at http://localhost:3000/graphql reason: Unexpected token < in JSON at position 0
        FetchError: invalid json response body at http://localhost:3000/graphql reason: Unexpected token < in JSON at position 0
    at C:\ngWishList2\node_modules\node-fetch\lib\index.js:271:32
    at processTicksAndRejections (internal/process/task_queues.js:94:5)
    at async UrlLoader.load (C:\ngWishList2\node_modules\@graphql-toolkit\url-loader\index.cjs.js:87:22)
    at async C:\ngWishList2\node_modules\@graphql-toolkit\core\index.cjs.js:682:25        
    at async Promise.all (index …
Run Code Online (Sandbox Code Playgroud)

graphql nestjs graphql-codegen

6
推荐指数
1
解决办法
8607
查看次数

AWS Amplify CLI 生成的 GraphQL 突变中的 $condition 输入参数是什么?

我已从此模型在 AWS AppSync 上(使用 CLI)生成了一个简单的 GraphQL API:

type WalletProperty @model {
    id: ID!
    title: String!
}
Run Code Online (Sandbox Code Playgroud)

这生成了 CreateWalletProperty、UpdateWalletProperty 和 DeleteWalletProperty 突变,所有这些都与此类似:

  mutation CreateWalletProperty(
    $input: CreateWalletPropertyInput!
    $condition: ModelWalletPropertyConditionInput    <<<<<<<<<<<<  what is this for?
  ) {
    createWalletProperty(input: $input, condition: $condition) {
      id
      title
      createdAt
      updatedAt
    }
  }
Run Code Online (Sandbox Code Playgroud)

条件的模式是:

input ModelWalletPropertyConditionInput {
  title: ModelStringInput
  and: [ModelWalletPropertyConditionInput]
  or: [ModelWalletPropertyConditionInput]
  not: ModelWalletPropertyConditionInput
}
Run Code Online (Sandbox Code Playgroud)

鉴于我总是必须提供强制性的 $input,$condition 参数有什么用?

graphql aws-appsync graphql-mutation graphql-codegen

6
推荐指数
1
解决办法
3858
查看次数

自动生成函数的类型安全包装,然后仅使用`__typename` 作为参数动态调用。打字稿

我拥有由 awesome 完全类型安全的自动生成代码graphql-codgen/vue。我通过构建一个小包装器在我的项目中使用它,因此我的用户不必每次调用都执行常见的配置任务。例如定义缓存行为、自动更新缓存、以正确的类型和格式解构结果。

带有 JS 和 with 的包装器工人,any但我也希望它是类型安全的,并且由于graphql-codegen已经以类型安全的方式生成了所有类型和方法,我认为必须有一种方法可以做到这一点。不知何故,我认为有歧视性的工会......

所以归结为示例代码我的问题是:我有这个自动生成的代码:

//File GQLService.ts
export type CustodiansList = (
  { __typename: 'Query' }
  & { custodiansList?: Maybe<Array<(
    { __typename: 'Custodian' }
    & Pick<Custodian, 'id' | 'name' | 'street' | 'zip' | 'city' | 'telephone' | 'createdAt' | 'updatedAt'>
  )>> }
);

type ReactiveFunctionCustodiansList = () => CustodiansListVariables

/**
 * __useCustodiansList__
 *
 * To run a query within a Vue component, call `useCustodiansList` and pass it any options …
Run Code Online (Sandbox Code Playgroud)

typescript vue.js graphql graphql-codegen

5
推荐指数
1
解决办法
199
查看次数

带插值的 graphql-codegen 动态字段

我正在使用graphql-codegen从 graphql 模式生成打字稿类型。我正在尝试创建一个带有动态字段的片段。

\n

模式.ts

\n

这是由 生成的类型graphql-codegen

\n
/** User Type */\nexport type UserType = {\n  __typename?: \'UserType\';\n  id: Scalars[\'ID\'];\n  avatar: Scalars[\'String\'];\n  email: Scalars[\'String\'];\n  name: Scalars[\'String\'];\n  showPostsInFeed: Scalars[\'Boolean\'];\n  username: Scalars[\'String\'];\n};\n
Run Code Online (Sandbox Code Playgroud)\n

用户模型.ts

\n

我在整个应用程序中使用这些接口来实现验证和一致性。

\n
export interface IUserBase {\n  id: string;\n  avatar: string;\n  name: string;\n  username: string;\n}\n\nexport interface IUserPost extends IUserBase {\n  showPostsInFeed: boolean;\n}\n\nexport interface IUserProfile extends IUserBase, IUserPost {\n  email: string;\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n

显示.ts

\n

这是用于生成的文件。在这里,我想使用我的现有字段IUserPostIUserProfile接口制作一个带有动态字段的片段,以便重用这些字段并避免在片段中一一重复它们。

\n
import gql from \'graphql-tag\';\nimport …
Run Code Online (Sandbox Code Playgroud)

typescript graphql angular graphql-tag graphql-codegen

5
推荐指数
1
解决办法
3340
查看次数

如果我不使用 amplify,如何从 AppSync GraphQL 架构生成 Typescript 定义?

我使用 aws-cdk 设置了 AppSync api,但没有使用他们的 amplify 框架。我试图弄清楚如何/是否可以schema.graphql在不使用 amplify 的情况下从 AppSync 文件生成 Typescript 定义,即无法访问amplify codegen命令。我确实尝试安装并运行它,但我假设 amplify 期望文件位于某些目录中,因此失败。

我查看了https://graphql-code-generator.com但由于 AppSync 使用的特殊类型(如 )而无法工作AWSDateTime,解决此问题的方法是发布 api 并从 graphql 端点获取架构,但这并不理想,即我希望能够在本地生成这些类型,而无需发布架构。

这可行吗?

typescript graphql aws-appsync aws-amplify graphql-codegen

5
推荐指数
1
解决办法
1464
查看次数

生成的类型不适合解析器

我对 graphql-code-generator 有问题。生成的类型不适合解析器。我在行中出错viewer: () => ({

输入 '() => { createUser: () => { lastName: string; 名字:字符串;昵称:字符串;电子邮件:字符串;性别:用户性别;年龄:字符串;}; }' 不能分配给类型 'Resolver<ResolverTypeWrapper<Pick<ViewerMutation, "__typename"> & { createUser: UnconfirmedUser | 承诺 | 信件未发送 | 承诺<...> | 警告重复电子邮件 | 承诺<...>; }>、{}、MyContext、{}>'。

这是我的代码

代码生成文件

overwrite: true
schema: "http://localhost:8000/graphql"
documents: null
generates:
  src/toolses/generated/resolversTypes.ts:
    plugins:
      - "typescript"
      - "typescript-resolvers"
    config:
      useIndexSignature: true
      contextType: ../../resolvers/typescript-resolvers#MyContext
      customResolverFn: ../../resolvers/typescript-resolvers#customResolverFn
Run Code Online (Sandbox Code Playgroud)

typescript-resolvers.ts

export type MyContext = {
  res: Response;
  req: Request;
  admin: any;
  currentUser: string;
  models: any;
};

export type customResolverFn< …
Run Code Online (Sandbox Code Playgroud)

node.js typescript graphql graphql-codegen

5
推荐指数
0
解决办法
90
查看次数

有没有办法使用 GraphQl Codegen 删除 Maybe 和标量?

我使用带有typescripttypescript-operations插件的 GraphQl Codegen,但生成的文件包含 Maybe 和 Scalars 的色调,使文件看起来非常混乱。

export type Formation = {
  availability?: Maybe<Scalars['String']>;
  certificate?: Maybe<Scalars['String']>;
  description?: Maybe<Scalars['String']>;
  id: Scalars['ID'];
  imageId?: Maybe<Scalars['ID']>;
  imageUrl?: Maybe<Scalars['String']>;
  languageIds?: Maybe<Array<Scalars['ID']>>;
  languages?: Maybe<Array<Maybe<Taxonomy>>>;
  levelId?: Maybe<Scalars['ID']>;
  location?: Maybe<Taxonomy>;
  locationId?: Maybe<Scalars['ID']>;
  objective?: Maybe<Scalars['String']>;
  professionIds?: Maybe<Array<Scalars['ID']>>;
  requirementIds?: Maybe<Array<Scalars['ID']>>;
  sectorIds?: Maybe<Array<Scalars['ID']>>;
  tagIds: Array<Scalars['ID']>;
  title: Scalars['String'];
  typeId?: Maybe<Scalars['ID']>;
  updatedAt: Scalars['String'];
  urlFormation?: Maybe<Scalars['String']>;
};
Run Code Online (Sandbox Code Playgroud)

是否可以输出原始类型:

string | null
Run Code Online (Sandbox Code Playgroud)

代替:

Maybe<Scalars["String"]>
Run Code Online (Sandbox Code Playgroud)

谢谢!

javascript reactjs graphql graphql-codegen

5
推荐指数
0
解决办法
547
查看次数

使用 Typescript 加载器生成 GraphQL 代码

我如何使用打字稿加载器graphql-codegen?我需要动态构建我的 graphql 模式,因此我必须从打字稿文件加载它。

\n

这是我正在使用的文件:

\n

代码生成.yaml

\n
generates:\n  src/generated/auth-resolvers.types.ts:\n    schema: \n      - authTypeDefs:\n        loader: ./src/graphql/auth.typedefs.ts\n    config:\n      useIndexSignature: true\n    plugins:\n      - typescript\n      - typescript-resolvers\nrequire:\n  - ts-node/register\n  - tsconfig-paths/register\n
Run Code Online (Sandbox Code Playgroud)\n

auth.typedefs.ts

\n
import { Permission } from "@src/generated/model.types";\nimport { gql } from "apollo-server";\nimport { makeExecutableSchema } from "graphql-tools";\n\nlet permFields = "";\nfor (const perm in Permission) {\n  permFields += `  ${perm}: PermissionDomains\\n`;\n}\n\nexport default gql`\ntype JsonType {\n  json: Json\n}\n\ntype BooleanType {\n  boolean: Boolean\n}\n\nunion PermissionOperationDomain = JsonType | BooleanType\n\ntype SecurityDomain {\n  read: …
Run Code Online (Sandbox Code Playgroud)

typescript graphql graphql-codegen

5
推荐指数
0
解决办法
778
查看次数

如何从 GraphQL 模式生成 GraphQL 操作

我正在寻找一种在使用 GraphQL 时使开发人员工作流程更高效的方法。

目前,graphql-code-generator用于从前端的 GraphQL 服务器生成类型。

这很好,但这只是生成类型。为了生成突变、查询和订阅的方法,我需要为前端项目中的每个操作创建一个 GraphQL 文档,例如:

file addPost.graphql

mutation addPost {
...
}
...
Run Code Online (Sandbox Code Playgroud)

我发现必须创建一个额外的addPost.graphql来生成该方法有点多余,因为它已经在我的 GraphQL 服务器上声明。

是否有插件/配置可以生成我可以在我的项目中使用的这些方法,而无需手动创建额外的 GraphQL 文档?

这是我的 GraphQL 生成器 yml 文件

# graphql-generator.yml
overwrite: true
schema: https://localhost:8088/query
documents: ./libs/**/*.graphql          <----- generating these *.graphql files would be great! 
generates:
  libs/graphql/src/lib/generated/graphql.ts:
    plugins:
      - "typescript"
      - "typescript-resolvers"
      - "typescript-operations"
      - "typescript-apollo-angular"
  ./graphql.schema.json:
    plugins:
      - "introspection"

Run Code Online (Sandbox Code Playgroud)

typescript graphql apollo-client graphql-codegen

4
推荐指数
1
解决办法
1648
查看次数