如何从 dart 文件调用 graphQl "query"
How to call a graphQl "query" from dart file
我正在尝试在 flutter 中使用 graphQL 调用网络调用(GraphQL - 查询),我需要知道实现此目的的确切方法。
请使用这个包https://pub.dev/packages/graphql_flutter
例如
https://pub.dev/packages/graphql_flutter#queries
String readRepositories = """
query ReadRepositories($nRepositories: Int!) {
viewer {
repositories(last: $nRepositories) {
nodes {
id
name
viewerHasStarred
}
}
}
}
""";
Query(
options: QueryOptions(
document: readRepositories, // this is the query string you just created
variables: {
'nRepositories': 50,
},
pollInterval: 10,
),
// Just like in apollo refetch() could be used to manually trigger a refetch
builder: (QueryResult result, { VoidCallback refetch }) {
if (result.errors != null) {
return Text(result.errors.toString());
}
if (result.loading) {
return Text('Loading');
}
// it can be either Map or List
List repositories = result.data['viewer']['repositories']['nodes'];
return ListView.builder(
itemCount: repositories.length,
itemBuilder: (context, index) {
final repository = repositories[index];
return Text(repository['name']);
});
},
);
你也可以直接测试例子
https://github.com/zino-app/graphql-flutter/tree/master/examples/starwars
我正在尝试在 flutter 中使用 graphQL 调用网络调用(GraphQL - 查询),我需要知道实现此目的的确切方法。
请使用这个包https://pub.dev/packages/graphql_flutter
例如 https://pub.dev/packages/graphql_flutter#queries
String readRepositories = """
query ReadRepositories($nRepositories: Int!) {
viewer {
repositories(last: $nRepositories) {
nodes {
id
name
viewerHasStarred
}
}
}
}
""";
Query(
options: QueryOptions(
document: readRepositories, // this is the query string you just created
variables: {
'nRepositories': 50,
},
pollInterval: 10,
),
// Just like in apollo refetch() could be used to manually trigger a refetch
builder: (QueryResult result, { VoidCallback refetch }) {
if (result.errors != null) {
return Text(result.errors.toString());
}
if (result.loading) {
return Text('Loading');
}
// it can be either Map or List
List repositories = result.data['viewer']['repositories']['nodes'];
return ListView.builder(
itemCount: repositories.length,
itemBuilder: (context, index) {
final repository = repositories[index];
return Text(repository['name']);
});
},
);
你也可以直接测试例子 https://github.com/zino-app/graphql-flutter/tree/master/examples/starwars