2016-05-20 27 views
6

我有一个页面使用GraphQL中继连接取得draftsGraphQL中继突变配置RANGE_ADD的连接父母名称

query { 
    connections { 
     drafts(first: 10) { 
      edges { 
       node { 
        ... on Draft { 
         id 
        } 
       } 
      } 
     } 
    } 
} 

在这个页面中,我也通过CreateDraftMutation创建草稿。

mutation { 
    createDraft(input: { 
     clientMutationId: "1" 
     content: "content" 
    }) { 
     draft { 
      id 
      content 
     } 
    } 
} 

在此突变后,我希望Relay将创建的草稿添加到其存储中。突变配置的最佳人选是RANGE_ADD,这是记录如下:

https://facebook.github.io/relay/docs/guides-mutations.html

RANGE_ADD 鉴于父母,连接和新创建的边缘的名称在响应负载继电器将增加节点到商店并根据指定的范围行为将其附加到连接。

参数

parentName:串 在响应中的字段名,表示连接的父

的parentID:字符串 包含连接

connectionName中的父节点的数据ID:字符串 表示连接的响应中的字段名称

edgeName:string 在响应中的字段名,表示新创建的边缘

rangeBehaviors:{[呼叫:字符串]:GraphQLMutatorConstants.RANGE_OPERATIONS}

之间印刷,点分隔GraphQL按字母顺序调用的地图,并且我们希望在这些调用影响下将新边缘添加到连接时展现的行为。行为可以是'append','ignore','prepend','refetch'或'remove'之一。

从文档的例子都按如下:

class IntroduceShipMutation extends Relay.Mutation { 
    // This mutation declares a dependency on the faction 
    // into which this ship is to be introduced. 
    static fragments = { 
    faction:() => Relay.QL`fragment on Faction { id }`, 
    }; 
    // Introducing a ship will add it to a faction's fleet, so we 
    // specify the faction's ships connection as part of the fat query. 
    getFatQuery() { 
    return Relay.QL` 
     fragment on IntroduceShipPayload { 
     faction { ships }, 
     newShipEdge, 
     } 
    `; 
    } 
    getConfigs() { 
    return [{ 
     type: 'RANGE_ADD', 
     parentName: 'faction', 
     parentID: this.props.faction.id, 
     connectionName: 'ships', 
     edgeName: 'newShipEdge', 
     rangeBehaviors: { 
     // When the ships connection is not under the influence 
     // of any call, append the ship to the end of the connection 
     '': 'append', 
     // Prepend the ship, wherever the connection is sorted by age 
     'orderby(newest)': 'prepend', 
     }, 
    }]; 
    } 
    /* ... */ 
} 

如果父是派那样明显,这是小菜一碟,但我一直有很难识别parentName和parentID,如果它直接来自查询连接。

我该怎么做?

编辑:

这是如何导出查询。

export default new GraphQLObjectType({ 
    name: 'Query', 
    fields:() => ({ 
    node: nodeField, 
    viewer: { 
     type: viewerType, 
     resolve:() => ({}), 
    }, 
    connections: { 
     type: new GraphQLObjectType({ 
     name: 'Connections', 

这反过来继电器容器

export default Relay.createContainer(MakeRequestPage, { 
    fragments: { 
     connections:() => Relay.QL` 
      fragment on Connections { 
+0

在您的查询中连接是一个GraphQL对象还是连接? –

+0

@AhmadFerdousBinAlam我添加了下面的代码片段。 'Connections'GraphQLObjectType带有连接字段的字段。 – lustdante

回答

1

我一直有很难识别parentName和的parentID 如果 直接从查询连接来使用。

faction和从中继文档的例子ships是完全一样的,你的情况connectionsdrafts。每个GraphQLObject都有一个ID,您的connections对象也有。因此,对于您的突变,parentNameconnctionsparentIDconnections的ID。

query { 
    connections { 
     id 
     drafts(first: 10) { 
      edges { 
       node { 
        ... on Draft { 
         id 
        } 
       } 
      } 
     } 
    } 
} 

顺便说一句,我想从你的应用领域connectionsdrafts的条款。否则,connections会与GraphQL连接类型混淆。

+0

为了举例,'drafts'绝对是应用领域术语。我刚刚意识到'连接',我把它当作常用的根查询,比如'node'或'viewer',对我们的应用来说也是独一无二的。 (我的队友想出了它)。所以我猜连接id应该像一个全局ID。 – lustdante

+0

是的,这个ID是一个全局ID。 –

+0

如果您直接在查询下有草稿,那么parentName和parentId是什么? – velop