反应中的模拟服务和运行参数化查询apollo graphql

时间:2017-10-05 18:44:46

标签: apollo react-apollo apollo-client

我正在尝试模拟服务,例如文档示例http://dev.apollodata.com/tools/graphql-tools/mocking.html#Default-mock-example

我正在使用标准内省查询导出我的架构,构建架构对象并在尝试运行文档中提到的查询时使用此对象。

import * as introspectionResult from "../../graphql/schema.json"
const GRAPHQL_QUERY = `
query eventsFeatured(
  $type: String!,
  $entity_type: String,
  $market__role: String,
  $outcome__odds_rank_unique: Int,
  $limit: Int
) {
  featured(
    type: $type,
    entity_type: $entity_type,
    market__role: $market__role,
    outcome__odds_rank_unique: $outcome__odds_rank_unique,
    limit: $limit
  ) {
    ...FeaturedFragment
    media {
      description
      extralarge
      large
      medium
      preview
      __typename
    }
    event {
      ...EventFragment
      market {
        ...MarketFragment
        outcome {
          ...OutcomeFragment
          media_logo {
            preview
            small
            __typename
          }
          bookie {
            ...BookieFragment
            __typename
          }
          __typename
        }
        __typename
      }
      article {
        ...ArticleFragment
        __typename
      }
      category3 {
        ...AllCategoriesFragment
        __typename
      }
      __typename
    }
    offer {
      id
      name
      conditions
      deeplink
      domain {
        ...EventFragment
        __typename
      }
      __typename
    }
    __typename
  }
}

fragment EventFragment on Event {
  id
  name
  canonicalised_name
  display_name
  date
  date_human
  date_short
  type
  __typename
}

fragment MarketFragment on Market {
  id
  name
  display_name
  canonicalised_name
  type
  role {
    name
    __typename
  }
  __typename
}

fragment OutcomeFragment on Outcome {
  id
  name
  display_name
  canonicalised_name
  odds
  odds_decimal
  odds_rank
  deeplink
  home_or_away
  type
  __typename
}

fragment BookieFragment on Bookie {
  name
  __typename
}

fragment ArticleFragment on BaseArticle {
  title
  body
  excerpt
  author
  date_no_time
  date_stamp
  date_human
  date_short
  date
  __typename
}

fragment AllCategoriesFragment on Category3 {
  ...Category3Fragment
  category2 {
    ...Category2Fragment
    category1 {
      ...Category1Fragment
      __typename
    }
    __typename
  }
  __typename
}

fragment Category1Fragment on Category1 {
  name
  canonicalised_name
  __typename
}

fragment Category2Fragment on Category2 {
  name
  canonicalised_name
  __typename
}

fragment Category3Fragment on Category3 {
  name
  canonicalised_name
  __typename
}

fragment FeaturedFragment on Featured {
  id
  type
  display_order
  __typename
}
`

// this builds our mock apollo schema
const schema = buildClientSchema(introspectionResult) 
addMockFunctionsToSchema({ schema })

// execute the supplied apollo query
    graphql(schema, GRAPHQL_QUERY, {
      options: (props) => ({
        variables: {
          type: "IMAGEGRIDHOMEPAGE"
        },
      })})
      .then(graphqlResult => {
.... etc
```

However, I am seeing the following error:

```
{ errors:
       [ GraphQLError {
           message: 'Variable "$type" of required type "String!" was not provided.',
           locations: [Object],
           path: undefined } ] }

似乎graphql函数无法识别标准选项对象,在此上下文中使用variables定义时,将架构对象用作第一个arg。我试过在这里挖掘源代码,但我的打字稿不是最好的 - https://github.com/apollographql/react-apollo/blob/master/src/graphql.tsx

1 个答案:

答案 0 :(得分:0)

用户错误 - 从文档中可以看出,graphql函数来自基础graphql库,而不是apollo-react库。