英文:
Type errors using Apollo Client in React Native typescript project
问题
我正在尝试在我的React Native项目中使用Apollo Client。我尝试根据官方教程从graphql API生成类型:https://www.apollographql.com/docs/react/development-testing/static-typing/
问题是,我正在使用返回unknown
的gql
函数。这在useQuery
和useMutation
钩子中造成问题。
一个已定义的mutation(在Apollo playground中工作):
export const registerMutation = gql(`
mutation Register($email: String!, $password: String!, $companyName: String) {
register(data:{email: $email, password: $password, companyName: $companyName}){
name
email
profileImage
}
}
`);
使用这个mutation:
const [register] = useMutation(registerMutation);
我从useMutation
钩子得到的错误:Argument of type 'unknown' is not assignable to parameter of type 'DocumentNode | TypedDocumentNode<any, OperationVariables>'.
英文:
I am trying to use Apollo Client in my React Native project. I tried to generate types from the graphql API based on the official tutorial: https://www.apollographql.com/docs/react/development-testing/static-typing/
The problem is that I am using the gql
function which returns unknown
. And these make problems in the useQuery
and useMutation
hooks.
A defined mutation (this is working in Apollo playground):
export const registerMutation = gql(`
mutation Register($email: String!, $password: String!, $companyName: String) {
register(data:{email: $email, password: $password, companyName: $companyName}){
name
email
profileImage
}
}
`);
Usage of the mutation:
const [register] = useMutation(registerMutation);
The error I am getting from useMutation hook: Argument of type 'unknown' is not assignable to parameter of type 'DocumentNode | TypedDocumentNode<any, OperationVariables>'.
答案1
得分: 1
我也遇到了相同的错误,我通过为mutation分配了一个类型来解决它,即在定义registerMutation
时可以分配一个类型。
例如,
import { TypedDocumentNode, gql } from "@apollo/client";
import {
MutationTokenAuthArgs,
ObtainJsonWebToken,
} from "src/__generated__/graphql";
export const FETCH_TOKEN: TypedDocumentNode<
ObtainJsonWebToken,
MutationTokenAuthArgs
> = gql`
mutation tokenAuth($email: String!, $password: String!) {
tokenAuth(email: $email, password: $password) {
refreshExpiresIn
token
}
}
`;
请注意,我使用了来自@apollo/client的gql,而不是由codegen生成的那个,而类型MutationTokenAuthArgs
和ObtainJsonWebToken
是由graphql codegen生成的。我通过在codegen.ts
文件中的generates
键下提供的文件夹中搜索mutation名称(在我的情况下是tokenAuth
)找到了它们。
源链接:
英文:
I was also getting the same error I resolved it by assigning a type to the mutation i.e. a type can be assigned to the registerMutation
where it is defined.
For example,
import { TypedDocumentNode, gql } from "@apollo/client";
import {
MutationTokenAuthArgs,
ObtainJsonWebToken,
} from "src/__generated__/graphql";
export const FETCH_TOKEN: TypedDocumentNode<
ObtainJsonWebToken,
MutationTokenAuthArgs
> = gql`
mutation tokenAuth($email: String!, $password: String!) {
tokenAuth(email: $email, password: $password) {
refreshExpiresIn
token
}
}
`;
Notice I have used gql from the @apollo/client and not the one that was generated by codegen and the types MutationTokenAuthArgs
, ObtainJsonWebToken
were generated by the graphql codegen. I searched them using the mutation name in my case tokenAuth
in the folder provided in codegen.ts
under the generates
key.
Link for the source:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论