Skip to content

Commit 38b1970

Browse files
author
Chad Kimes
authored
Merge pull request #52 from markhepburn/markhepburn-patch-1
Update examples in README
2 parents 1f41dd7 + cbee42d commit 38b1970

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

README.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ var schema = GraphQL<TestContext>.CreateDefaultSchema(() => new TestContext());
7171
The default schema is required to use the helper method`GraphQL<TContext>.Execute(query)`, but you can execute queries against the schema without it. Next, we'll define a type in the schema and fields on that type.
7272

7373
```csharp
74-
schema.AddType<User>()
75-
.AddField(u => u.Id)
76-
.AddField(u => u.Name)
77-
.AddField(u => u.Account)
78-
.AddField("totalUsers", (db, u) => db.Users.Count())
79-
.AddField("accountPaid", (db, u) => u.Account.Paid);
74+
var user = schema.AddType<User>();
75+
user.AddField(u => u.Id);
76+
user.AddField(u => u.Name);
77+
user.AddField(u => u.Account);
78+
user.AddField("totalUsers", (db, u) => db.Users.Count());
79+
user.AddField("accountPaid", (db, u) => u.Account.Paid);
8080
```
8181

8282
Fields can be defined using only a property expression, or you can specify your own fields and provide a custom resolving expression. Let's do the same for account:
@@ -87,14 +87,20 @@ schema.AddType<Account>().AddAllFields();
8787

8888
If we just want to expose all fields, we can use the `AddAllFields` helper method.
8989

90-
The last thing we want to do is create some queries. Let's add some to find users:
90+
The last thing we want to do is create some queries, as fields on the schema itself. Let's add some to find users:
9191

9292
```csharp
93-
schema.AddQuery("users", db => db.Users);
94-
schema.AddQuery("user", new { id = 0 }, (db, args) => db.Users.Where(u => u.Id == args.id).FirstOrDefault());
93+
schema.AddListField("users", db => db.Users);
94+
schema.AddField("user", new { id = 0 }, (db, args) => db.Users.Where(u => u.Id == args.id).FirstOrDefault());
9595
```
9696

97-
In our first query, we want to see all users so we can just return the entire list. However, notice how in the second query we define the shape of an anonymous type `new { id = 0 }`. This is what is expected to be passed in from the GraphQL query. Since we've defined the shape, we can now use that in the `Where` clause to build our IQueryable. We use `FirstOrDefault` to signify that this query will return a single result. Now we're ready to execute a query.
97+
In our first query, we want to see all users so we can just return the entire list. However, notice how in the second query we define the shape of an anonymous type `new { id = 0 }`. This is what is expected to be passed in from the GraphQL query. Since we've defined the shape, we can now use that in the `Where` clause to build our IQueryable. We use `FirstOrDefault` to signify that this query will return a single result.
98+
99+
```csharp
100+
schema.Complete();
101+
```
102+
103+
Finally, we complete the schema when we've finished setting up. Now we're ready to execute a query.
98104

99105
## Executing Queries
100106

@@ -110,7 +116,8 @@ user(id:1) {
110116
totalUsers
111117
}";
112118

113-
var dict = GraphQL<TestContext>.Execute(query);
119+
var gql = new GraphQL<TestContext>(schema);
120+
var dict = gql.ExecuteQuery(query);
114121
Console.WriteLine(JsonConvert.SerializeObject(dict, Formatting.Indented));
115122

116123
// {

0 commit comments

Comments
 (0)