https://blog.csdn.net/qq_31851435/article/details/102853649
1、安装CLI
$ yarn add -D graphql @graphql-codegen/cli
2、初始化
$ graphql-codegen init
gql文件示例
//String$backendIds
backendIds
namesellerPhone
accountType
id
disabled
}
}
// current-user.gqlquery CurrentUser {currentUser {roleliveRoom {
id
name
}
}
}
初始化命令执行后会在根目录下生成 codegen.yml,这个文件可以根据 graphql 的接口自动生成调用方法,生成文件在 libs/xxx/graphql/documents.ts(这个地址是你自己项目里配置的地址),然后直接引用里面的方法来调接口
// codegen.yml
overwrite: true
schema: "http://localhost:3333/xxx-api/graphql"
documents: "libs/xxx/graphql/**/*.{gql,graphql}"
generates:
libs/xxx/graphql/documents.ts:
plugins:
- "typescript"
- "typescript-operations"
- "typescript-apollo-angular"
注意:接口地址必须指定正确,否则执行命令会报错
3、执行生成命令
// package.json
flutter下相关文件格式
// codegen.yml
schema: "相关网址接口"
overwrite: true
generates:
../lib/gql(自定义生成gql)/model.dart:
plugins:
- graphql-to-dart
config:
requiredFields: false
parts:
- "model.g.dart"
generateFragmentHelpers:
excludeFields:
- onType: Query
- onType: Mutation
irreducibleTypes:
-