该问题可能是由于使用了错误的语法或参数格式引起的。为了正确传递参数,需要使用正确的语法。
以下是一个示例代码,展示如何在进行GQL REST查询时正确传递参数:
import { ApolloClient, createHttpLink, InMemoryCache } from '@apollo/client';
import { RestLink } from 'apollo-link-rest';
const restLink = new RestLink({
uri: 'https://api.example.com',
endpoints: {
users: 'https://api.example.com/users/:userId',
},
});
const httpLink = createHttpLink({
uri: 'https://api.example.com/graphql',
});
const client = new ApolloClient({
cache: new InMemoryCache(),
link: restLink.concat(httpLink),
});
const GET_USER_QUERY = gql`
query getUser($userId: String!) {
user @rest(type: "User", path: "users/{args.userId}") {
id
name
email
}
}
`;
client.query({
query: GET_USER_QUERY,
variables: {
userId: "123",
},
}).then(response => console.log(response.data.user));
在此示例中,我们将通过在查询中传递userId
参数来获取用户数据。使用ApolloClient
的query
方法,同时传递查询和变量,可以正确地将参数传递给GQL REST查询,以获取正确的结果。