我正在使用 React、Apollo 和 Prsima 构建一个应用程序,允许用户按型号、品牌、价格过滤汽车......我知道(例如)如何按品牌过滤汽车:
const GET_CARS = gql`
query FilterCars($brandId: ID){
cars(where: {
model : { brand: { id: $brandId } }
}) {
id
model {
name
brand {
name
}
horses
}
year
km
price
...
}
`;
Run Code Online (Sandbox Code Playgroud)
并在组件中:
const CarList = (props) => {
const { data, loading, error } = useQuery(GET_CARS, {
variables: {
brandId: "exampleBrandId"
}
})
...
}
Run Code Online (Sandbox Code Playgroud)
问题是,有些参数是可选的:也许用户不关心品牌、型号、价格……那么所有汽车都应该出现:如果没有选择品牌,则应该出现所有品牌的汽车;如果未选择价格,则应出现所有价格的汽车...
我怎样才能做到这一点?就像是:
query FilterCars($brandId: ID){
cars(where: {
model : { brand: { id: $brandId || …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
const client: ApolloClient<any> = new ApolloClient({
uri: MY_URI,
});
Run Code Online (Sandbox Code Playgroud)
我想替换any为一个TCache类型,如删除时弹出的 VSCode 工具提示所示any:
问题是TCache无法从以下位置导入apollo-client:
import { TCache } from "apollo-client"; // does NOT work
Run Code Online (Sandbox Code Playgroud)
我还克隆了他们的存储库并试图找到 的定义TCache,但没有。
尝试将嵌套变量传递给 GraphQL 查询,但我的服务器仅获取顶级变量 ( shopId),其他所有内容均为 null。
我试过:
#1
const CALCULATE_PACKAGE_PRICE = gql`
query CalculatePackagePrice(
$shopId: String!
$address1: String
$zip: String
$city: String
$countryCode: String
) {
calculatePackagePrice(
where: {
shopId: $shopId
destination: {
address1: $address1
zip: $zip
city: $city
countryCode: $countryCode
}
}
) {
name
price
userErrors {
field
message
}
}
}
`
const [calculatePackagePrice, { loading, data }] = useLazyQuery(
CALCULATE_PACKAGE_PRICE,
{
variables: {
shopId: shopId,
destination: {
address1: "Example 123",
zip: "123",
city: "Test",
countryCode: …Run Code Online (Sandbox Code Playgroud)从 useEffect 挂钩接收数据后,我尝试调用 Graph QL 查询。我需要响应中的数据在查询中使用。然而,钩子不能有条件地调用。然而,如果我去掉这个条件,loadedAnime 将是未定义的。我该如何摆脱这种限制?
import React, { useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import AnimeBanner from "../components/AnimeBanner";
import { useHttpClient } from "../Hooks/http-hook";
import { GetAnimeData } from "../GraphQLFunctions";
import { useQuery } from "@apollo/react-hooks";
import gql from "graphql-tag";
const GET_ANIME_INFO = gql`
query GetAnimeInfo($name: String!) {
Media(search: $name) {
title {
romaji
english
native
userPreferred
}
episodes
id
bannerImage
}
}
`;
const Anime = (props) => {
//Logic for getting …Run Code Online (Sandbox Code Playgroud) 我有一个需要输入参数的查询,比如日期
const { loading, data, error } = useQuery(GET_LIST, {
variables: {
input: {
date: date,
},
},
});
Run Code Online (Sandbox Code Playgroud)
export const GET_LIST = gql`
query list($input: ListParams) {
list(input: $input) {
totalCount
recordCount
list {
listId
date
qty
amount
currency
}
}
}
`;
Run Code Online (Sandbox Code Playgroud)
input ListParams {
date: String
}
Run Code Online (Sandbox Code Playgroud)
我需要获取列表,用户可以根据日期进行过滤。现在,在初始加载时,未设置日期,而是调用查询。用户设置日期,没有问题,使用日期值再次调用查询,现在当用户删除日期过滤器时,日期值变得未定义,我希望这次再次调用 useQuery ,没有变量,但它从未被调用过。
我也尝试过设置空字符串,即使这样 useQuery 也不会被调用,这不是预期的行为
input: {
date: date||'',
},
Run Code Online (Sandbox Code Playgroud) 我为 Apollo 客户端设置了以下 ErrorLink。
export const errorLink = onError(
({ response, graphQLErrors, networkError, operation }: ErrorResponse) => {
notificationService.notify("An Error Occurred");
},
);
Run Code Online (Sandbox Code Playgroud)
我需要在单元测试中测试这个实现。我有以下内容来测试 Apollo Links
const MockQuery = gql`
query {
foo
}
`;
interface LinkResult<T> {
operation: Operation;
result: FetchResult<T>;
}
async function executeLink<T = ApolloLink>(
linkToTest: ApolloLink,
request: GraphQLRequest = { query: MockQuery },
) {
const linkResult = {} as LinkResult<T>;
return new Promise<LinkResult<T>>((resolve, reject) => {
execute(ApolloLink.from([linkToTest]), request).subscribe(
(result) => {
linkResult.result = result as …Run Code Online (Sandbox Code Playgroud) ApolloError有networkError属性,但当服务器响应 4** 或 5** 状态代码时设置它。
以及如何检查问题是否是由网络连接不良引起的?
try {
apolloClient.query(someQuery)
} catch (error) {
if (isInternetConnectionError(error)) { // how to check this?
Alert.alert('Please check your internet connection!')
} else {
logException(error);
}
}
Run Code Online (Sandbox Code Playgroud) javascript exception internet-connection apollo apollo-client
我升级了 Nestjs 服务器包并尝试启动我的服务器。这次我得到了以下错误:
`TypeError: (0 , apollo_server_core_1.ApolloServerPluginLandingPageGraphQLPlayground) is not a function`
Run Code Online (Sandbox Code Playgroud)
我疑惑了...
我正在使用Sequelize连接到Postgres数据库.我有这个代码:
return Promise.resolve()
.then(() => {
console.log('checkpoint #1');
const temp = connectors.IM.create(args);
return temp;
})
.then((x) => console.log(x))
.then((args) =>{
console.log(args);
args = Array.from(args);
console.log('checkpoint #2');
const temp = connectors.IM.findAll({ where: args }).then((res) => res.map((item) => item.dataValues))
return temp;
}
)
.then(comment => {
return comment;
})
.catch((err)=>{console.log(err);});
Run Code Online (Sandbox Code Playgroud)
在检查点#1的第一个.then块中,新记录成功添加到Postgres数据库.在console.log(x)在未来再块,这被记录到控制台:
{ dataValues:
{ id: 21,
fromID: '1',
toID: '2',
msgText: 'Test from GraphIQL',
updatedAt: Wed Oct 12 2016 09:52:05 GMT-0700 (PDT),
createdAt: Wed Oct 12 2016 09:52:05 GMT-0700 (PDT) …Run Code Online (Sandbox Code Playgroud) 我目前正在为Apollo客户端使用vue-apollo包,并为我的GraphQl API使用带有django和graphene-python的VueJs堆栈。
我在下面用vue-apollo进行了简单的设置:
import Vue from 'vue'
import { ApolloClient } from 'apollo-client'
import { HttpLink } from 'apollo-link-http'
import { InMemoryCache } from 'apollo-cache-inmemory'
import VueApollo from 'vue-apollo'
import Cookies from 'js-cookie'
const httpLink = new HttpLink({
credentials: 'same-origin',
uri: 'http://localhost:8000/api/',
})
// Create the apollo client
const apolloClient = new ApolloClient({
link: httpLink,
cache: new InMemoryCache(),
connectToDevTools: true,
})
export const apolloProvider = new VueApollo({
defaultClient: apolloClient,
})
// Install the vue plugin
Vue.use(VueApollo)
Run Code Online (Sandbox Code Playgroud)
我还settings.py …
apollo ×10
graphql ×5
reactjs ×3
javascript ×2
node.js ×2
react-apollo ×2
django ×1
ecmascript-6 ×1
es6-promise ×1
exception ×1
gql ×1
prisma ×1
sequelize.js ×1
typescript ×1
unit-testing ×1
vue.js ×1