以下是一个示例代码,展示了如何在Apollo GraphQL中进行带参数的查询:
npm install apollo-boost graphql
import ApolloClient from 'apollo-boost';
const client = new ApolloClient({
uri: 'https://example.com/graphql', // 替换成你的 GraphQL 服务器的端点URL
});
import { gql } from 'apollo-boost';
const GET_USER = gql`
query GetUser($id: ID!) {
user(id: $id) {
id
name
email
}
}
`;
client.query({
query: GET_USER,
variables: { id: '123' }, // 替换成你想要查询的具体参数值
})
.then(result => {
console.log(result.data.user);
})
.catch(error => {
console.error(error);
});
在上述代码中,我们使用 variables
属性来传递参数值。在查询的定义中,我们使用了 $id: ID!
来声明参数类型和非空要求,并在查询的具体参数值中使用了 id: '123'
。
这样,你就可以在 Apollo GraphQL 中进行带参数的查询了。根据你的具体需求,可以自定义查询和参数,以适应你的数据模型和业务逻辑。