标签: apollo

具有动态键的对象的Apollo/GraphQL字段类型

假设我的graphql服务器想要获取以下数据作为JSON,其中person3person5是一些id:

"persons": {
  "person3": {
    "id": "person3",
    "name": "Mike"
  },
  "person5": {
    "id": "person5",
    "name": "Lisa"
  }
}
Run Code Online (Sandbox Code Playgroud)

问题:如何使用apollo创建模式类型定义?

密钥person3person5这里是动态生成的,具体取决于我的查询(即area在查询中使用).因此,在其他时间我可能会person1,person2,person3返回.如您所见persons,不是Iterable,因此以下不能用作我用apollo做的graphql类型定义:

type Person {
  id: String
  name: String
}
type Query {
  persons(area: String): [Person]
}
Run Code Online (Sandbox Code Playgroud)

persons对象中的键可能总是不同的.

当然,一种解决方案是将传入的JSON数据转换为使用数组persons,但是没有办法处理数据吗?

apollo graphql apollo-server

13
推荐指数
1
解决办法
6609
查看次数

在多种类型上使用GraphQL Fragment

如果我的GraphQL架构中有多个类型共有的字段集,有没有办法做这样的事情?

type Address {
  line1: String
  city: String
  state: String 
  zip: String
}

fragment NameAndAddress on Person, Business {
  name: String
  address: Address
}

type Business {
   ...NameAndAddress
   hours: String
}

type Customer {
   ...NameAndAddress
   customerSince: Date
}
Run Code Online (Sandbox Code Playgroud)

apollo graphql

13
推荐指数
2
解决办法
3847
查看次数

如何执行异步获取请求,然后重试上次失败的请求?

Apollo链接提供错误处理程序 onError

问题: 目前,我们希望在apollo调用期间过期时刷新oauth令牌,并且我们无法在onError正确的内部执行异步获取请求.

码:

initApolloClient.js

import { ApolloClient } from 'apollo-client';
import { onError } from 'apollo-link-error';
import { ApolloLink, fromPromise } from 'apollo-link';

//Define Http link
const httpLink = new createHttpLink({
    uri: '/my-graphql-endpoint',
    credentials: 'include'
});

//Add on error handler for apollo link

return new ApolloClient({
    link: ApolloLink.from([
        onError(({ graphQLErrors, networkError, operation, forward  }) => {
            if (graphQLErrors) {
                //User access token has expired
                if(graphQLErrors[0].message==="Unauthorized") {
                    //We assume we have both tokens needed to run the …
Run Code Online (Sandbox Code Playgroud)

apollo react-apollo apollo-client

13
推荐指数
3
解决办法
4429
查看次数

如何使用`apollo-server`加载.graphql文件?

我目前正在使用单独的.graphql文件加载 GraphQL 模式,但它封装在字符串中:

schema.graphql

const schema = `
  type CourseType {
    _id: String!
    name: String!
  }

  type Query {
    courseType(_id: String): CourseType
    courseTypes: [CourseType]!
  }
`

module.exports = schema
Run Code Online (Sandbox Code Playgroud)

然后将其用于apollo-server

index.js

const schema = `
  type CourseType {
    _id: String!
    name: String!
  }

  type Query {
    courseType(_id: String): CourseType
    courseTypes: [CourseType]!
  }
`

module.exports = schema
Run Code Online (Sandbox Code Playgroud)

有没有办法简单地加载一个看起来像这样的 .graphql ? schema.graphql

type CourseType {
  _id: String!
  name: String!
}

type Query {
  courseType(_id: String): CourseType
  courseTypes: [CourseType]! …
Run Code Online (Sandbox Code Playgroud)

node.js apollo graphql apollo-server

13
推荐指数
1
解决办法
9085
查看次数

ApacheActiveMQ和ApacheActiveMQApollo之间的区别

Apache ActiveMQ与Apache ActiveMQ Apollo有什么区别?

Apollo文档说:"ActiveMQ Apollo是一个更快,更可靠,更易于维护的消息代理,它是从原始ActiveMQ的基础构建的.它使用完全不同的线程和消息调度架构来实现这一点."但是没有java样本代码和良好的用户指南.

有人在实时环境中使用ApacheActiveMQApollo吗?对原始ActiveMQ做得更好吗?

java activemq-classic jms messagebroker apollo

12
推荐指数
1
解决办法
6012
查看次数

如何处理apollo-link-state中的嵌套状态(类似于Redux的combineReducers)

如果我们看一下todos示例,假设应用程序有多个视图(TodoList页面和另一个页面).

因此,不是直接引用todo项的数组的"todos",而是在状态/存储/缓存的顶层,它实际上只是具有某些自身状态的视图.

在该视图中,我们定义了待办事项列表和可见性过滤器 - 因此状态/存储/缓存不会如下所示:

{
  todos: [TodoItem]
  0:?TodoItem:0
  completed: false
  id: 0
  text: "hh"
  visibilityFilter: "SHOW_ALL"
}
Run Code Online (Sandbox Code Playgroud)

但是:

{ 
  scenes: {
    TodoList: {
      todos: [TodoItem]
      0:?TodoItem:0
      completed: false
      id: 0
      text: "hh"
      visibilityFilter: "SHOW_ALL"
    },
    SomeOtherView: { /* other state */}
  }
}
Run Code Online (Sandbox Code Playgroud)

它甚至可能被孤立在它自己的数据"模块"中,就像这里提出的那样:https://medium.com/@alexmngn/how-to-use-redux-on-highly-scalable-javascript-applications-4e4b8cb5ef38:

{ 
  scenes: {
    TodoList: {
      data: {
        todos: [TodoItem]
        0:?TodoItem:0
        completed: false
        id: 0
        text: "hh"
      }
      visibilityFilter: "SHOW_ALL"
    },
    SomeOtherView: { /* other state */}
  }
}
Run Code Online (Sandbox Code Playgroud)

应用程序范围的状态将进一步存储一个级别: …

apollo react-apollo apollo-client apollo-link-state

12
推荐指数
1
解决办法
1425
查看次数

等待流数据时Apollo Server超时

我正在尝试用我的Apollo Server等待流的结果.我的解析器看起来像这样.

async currentSubs() {
  try {
    const stream = gateway.subscription.search(search => {
      search.status().is(braintree.Subscription.Status.Active);
    });
    const data = await stream.pipe(new CollectObjects()).collect();
    return data;
  } catch (e) {
    console.log(e);
    throw new Meteor.Error('issue', e.message);
  }
},
Run Code Online (Sandbox Code Playgroud)

当返回的数据流很小时,这个解析器工作正常,但是当进入的数据较大时,我得到了一个503 (Service Unavailable).我看起来超时发生在30秒左右.我已经尝试过增加我的Express服务器的超时graphQLServer.timeout = 240000;但这并没有什么不同.

我怎样才能解决这个问题?30秒超时来自何处?只有在结果需要更长时间时才会失败.

我正在使用https://github.com/mrdaniellewis/node-stream-collect从流中收集结果.

来自try catch的错误:

I20180128-13:09:26.872(-7)? { proxy:
I20180128-13:09:26.872(-7)?    { error: 'Post http://127.0.0.1:26474/graphql: net/http: request canceled (Client.Timeout exceeded while awaiting headers)',
I20180128-13:09:26.872(-7)?      level: 'error',
I20180128-13:09:26.873(-7)?      msg: 'Error sending request to origin.',
I20180128-13:09:26.873(-7)?      time: '2018-01-28T13:09:26-07:00',
I20180128-13:09:26.873(-7)?      url: …
Run Code Online (Sandbox Code Playgroud)

braintree node.js apollo graphql apollo-server

12
推荐指数
1
解决办法
1766
查看次数

为什么我收到错误:无法在"查询"类型上查询字段xx?

虽然我在GraphiQL上成功测试了GraphiQL工具后复制并粘贴了graphQL查询,但当我在reactJS应用程序中的Apollo客户端中尝试时,查询返回错误:

[GraphQL error]: Message: Cannot query field "allStudents" on type "Query"., Location: [object Object], Path: undefined
Run Code Online (Sandbox Code Playgroud)

这是我的实现:

const link = createHttpLink({
  uri: 'http://localhost:8000/graphql',
  fetchOptions: { method: "POST" }
});

const client = new ApolloClient({
  link: link 
});

const GET_STUDENTS = gql`
query getStudents($schoolID: Int!){
  allStudents(schoolId: $schoolID){
    pickUpLat
    pickUpLng
  }
}
`;

client
  .query({
    query: GET_STUDENTS,
    variables: { schoolID: 1 }
  })
  .then(result => console.log(result));
Run Code Online (Sandbox Code Playgroud)

可能有什么不对?这是我预期的正确答案:

{
  "data": {
    "allStudents": [
      {
        "pickUpLat": 31.9752942479727,
        "pickUpLng": 35.8438429235775
      },
      {
        "pickUpLat": 31.9754545979993,
        "pickUpLng": …
Run Code Online (Sandbox Code Playgroud)

apollo reactjs graphql apollo-client

12
推荐指数
3
解决办法
8786
查看次数

包 apollo-link-http 中的 new HttpLink 和 createHttpLink 之间的差异

在教程中

https://www.howtographql.com/vue-apollo/1-getting-started/

有提供的new HttpLink语法,但在官方文档中

https://www.apollographql.com/docs/link/links/http/

函数createHttpLink被应用。

这两个来源都没有描述这些方法之间的差异。

apollo graphql-js apollo-client

12
推荐指数
1
解决办法
3008
查看次数

测试 useSubscription apollo hooks with react

测试useSubscription钩子我发现有点困难,因为该方法在Apollo 文档中被省略/未记录(在撰写本文时)。据推测,应该使用<MockedProvider />from来模拟它@apollo/react-testing,就像该链接中给出的示例中的突变一样。

测试我正在工作的订阅的加载状态:

成分:

const GET_RUNNING_DATA_SUBSCRIPTION = gql`
  subscription OnLastPowerUpdate {
    onLastPowerUpdate {
      result1,
      result2,
    }
  }
`;

const Dashboard: React.FC<RouteComponentProps & Props> = props => {
  const userHasProduct = !!props.user.serialNumber;

  const [startGetRunningData] = useMutation(START_GET_RUNNING_DATA);

  const [stopGetRunningData] = useMutation(STOP_GET_RUNNING_DATA);

  useEffect(() => {
    startGetRunningData({
      variables: { serialNumber: props.user.serialNumber },
    });

    return () => {
      stopGetRunningData();
    };
  }, [startGetRunningData, stopGetRunningData, props]);

  const SubscriptionData = (): any => {
    const { data, loading } …
Run Code Online (Sandbox Code Playgroud)

subscription apollo apollo-server apollo-client react-hooks

12
推荐指数
1
解决办法
2124
查看次数