簡體   English   中英

無法在反應中繼中獲取列表

[英]Unable to fetch list in react relay

我遵循與此處提到的相同的架構

我想獲取所有用戶,所以我更新了這樣的架構

var Root = new GraphQLObjectType({
  name: 'Root',
  fields: () => ({
    user: {
      type: userType,
      resolve: (rootValue, _) => {
        return getUser(rootValue)
      }
    },
    post: {
      type: postType,
      args: {
         ...connectionArgs,
          postID: {type: GraphQLString}
        },
      resolve: (rootValue, args) => {
       return getPost(args.postID).then(function(data){
        return data[0];
       }).then(null,function(err){
        return err;
       });
      }
    },

    users:{
      type: new GraphQLList(userType),
      resolve: (root) =>getUsers(),
    },
  })
});

在database.js中

export function getUsers(params) {
  console.log("getUsers",params)
  return new Promise((resolve, reject) => {
      User.find({}).exec({}, function(err, users) {
        if (err) {
          resolve({})
        } else {
          resolve(users)
        }
      });
  })
}

我在/ graphql中獲得結果

{
  users {
    id,
    fullName
  } 
}

和結果為

{
  "data": {
    "users": [
      {
        "id": "VXNlcjo1Nzk4NWQxNmIwYWYxYWY2MTc3MGJlNTA=",
        "fullName": "Akshay"
      },
      {
        "id": "VXNlcjo1Nzk4YTRkNTBjMWJlZTg1MzFmN2IzMzI=",
        "fullName": "jitendra"
      },
      {
        "id": "VXNlcjo1NzliNjcyMmRlNjRlZTI2MTFkMWEyMTk=",
        "fullName": "akshay1"
      },
      {
        "id": "VXNlcjo1NzliNjgwMDc4YTYwMTZjMTM0ZmMxZWM=",
        "fullName": "Akshay2"
      },
      {
        "id": "VXNlcjo1NzlmMTNkYjMzNTNkODQ0MmJjOWQzZDU=",
        "fullName": "test"
      }
    ]
  }
}

但是如果我試圖在視圖中獲取它

export default Relay.createContainer(UserList, {
  fragments: {
    userslist: () => Relay.QL`
      fragment on User @relay(plural: true) {
            fullName,
            local{
              email
            },
            images{
              full
            },
            currentPostCount,
            isPremium,
      }
    `,
  },
});

我收到錯誤Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings. Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.

請告訴我我錯過了什么。 我和@relay一起嘗試過很多次(復數:true)。 還嘗試使用參數更新模式

    users:{
      type: new GraphQLList(userType),
      args: {
        names: {
          type: GraphQLString,
        },
        ...connectionArgs,
      },
      resolve: (root, {names}) =>connectionFromArray(getUsers(names)),
    },

但是我得到了錯誤Cannot read property 'after' of undefined in implementing react-relay在此先感謝。

Relay目前僅支持三種類型的根域(請參閱facebook / relay#112 ):

  • 沒有參數的根字段,返回單個節點:
    • 例如{ user { id } } return {"id": "123"}
  • 帶有一個參數的根字段,返回單個節點:
    • 例如{ post(id: "456") { id } } return {"id": "456"}
  • 具有一個數組參數的根字段返回與參數數組具有相同大小的節點數組(也稱為“復數標識根字段”):
    • 例如{ users(ids: ["123", "321"]) { id } }返回[{"id": "123"}, {"id": "321"}]

解決方法是創建一個根域(通常稱為viewer ),返回具有這些字段的節點。 嵌套在Viewer (或任何其他節點)中時,允許字段具有任何返回類型,包括列表或連接。 當您在GraphQL服務器中包裝此對象中的字段時,您可以像這樣查詢它們:

{
  viewer {
    users {
      id,
      fullName,
    }
  }
}

Viewer類型是一個節點類型,因為它只有一個實例,所以它的id應該是一個常量。 您可以使用globalIdField幫助程序定義id字段,並添加要使用Relay查詢的任何其他字段:

const viewerType = new GraphQLObjectType({
  name: 'Viewer',
  interfaces: [nodeInterface],
  fields: {
    id: globalIdField('Viewer', () => 'VIEWER_ID'),
    users:{
      type: new GraphQLList(userType),
      resolve: (viewer) => getUsers(),
    },
  },
});

在客戶端上,您需要將路徑中的根查詢更改為{ viewer }並在Viewer上定義片段:

export default Relay.createContainer(UserList, {
  fragments: {
    viewer: () => Relay.QL`
      fragment on Viewer {
        users {
          fullName,
          local {
            email,
          },
          images {
            full,
          },
          currentPostCount,
          isPremium,
        }
      }
    `,
  },
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM