Skip to content

Commit 79c372d

Browse files
authoredApr 27, 2021
Added Fetching from Database documentation (#3600)
1 parent c774d90 commit 79c372d

File tree

1 file changed

+161
-5
lines changed

1 file changed

+161
-5
lines changed
 

‎website/src/docs/hotchocolate/fetching-data/fetching-from-databases.md

+161-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,168 @@
22
title: "Fetching from Databases"
33
---
44

5-
Hi,
5+
import { ExampleTabs } from "../../../components/mdx/example-tabs"
66

7-
We're currently working on the version 11 documentation. Probably right now at this very moment. However, this is an open-source project, and we need any help we can get! You can jump in at any time and help us improve the documentation for hundreds or even thousands of developers!
7+
In this section, you find a simple example on how you can fetch data from a database and expose it as a GraphQL API.
88

9-
In case you might need help, check out our slack channel and get immediate help from the core contributors or the community itself.
9+
**HotChocolate is not bound to a specific database, pattern or architecture.**
10+
[We do have a few integrations](/docs/hotchocolate/integrations), that help with a variety of databases, though these are just additions on top of HotChocolate.
11+
You can couple your business logic close to the GraphQL server, or cleanly decouple your domain layer from the GraphQL layer over abstractions.
12+
The GraphQL server only knows its schema, types and resolvers, what you do in these resolvers and what types you expose, is up to you.
13+
14+
In this example, we will directly fetch data from MongoDB in a resolver.
15+
16+
# Setting up the Query
17+
The query type in a GraphQL schema is the root type. Each field defined on this type is available at the root of a query.
18+
If a field is requested, the resolver of the field is called.
19+
The data of this resolver is used for further execution.
20+
If you return a scalar, value (e.g. `string`, `int` ...) the value is serialized and added to the response.
21+
If you return an object, this object is the parent of the resolver in the subtree.
22+
23+
<ExampleTabs>
24+
<ExampleTabs.Annotation>
25+
26+
```csharp
27+
// Query.cs
28+
public class Query
29+
{
30+
public Task<Book?> GetBookById(
31+
[Service] IMongoCollection<Book> collection,
32+
Guid id)
33+
{
34+
return collection.Find(x => x.Id == id).FirstOrDefaultAsync();
35+
}
36+
}
37+
38+
// Book.cs
39+
public class Book
40+
{
41+
public string Title { get; set; }
42+
43+
public string Author { get; set; }
44+
}
45+
46+
// Startup.cs
47+
public class Startup
48+
{
49+
public void ConfigureServices(IServiceCollection services)
50+
{
51+
services
52+
.AddRouting()
53+
.AddGraphQLServer()
54+
.AddQueryType<Query>();
55+
}
56+
57+
// Omitted code for brevity
58+
}
59+
```
60+
61+
</ExampleTabs.Annotation>
62+
<ExampleTabs.Code>
63+
64+
```csharp
65+
// Query.cs
66+
public class Query
67+
{
68+
public Task<Book?> GetBookById(
69+
[Service] IMongoCollection<Book> collection,
70+
Guid id)
71+
{
72+
return collection.Find(x => x.Id == id).FirstOrDefaultAsync();
73+
}
74+
}
75+
76+
// QueryType.cs
77+
public class QueryType : ObjectType<Query>
78+
{
79+
protected override void Configure(IObjectTypeDescriptor<Query> descriptor)
80+
{
81+
descriptor
82+
.Field(f => f.GetBookById(default!, default!))
83+
.Type<BookType>();
84+
}
85+
}
86+
87+
// Book.cs
88+
public class Book
89+
{
90+
public string Title { get; set; }
91+
92+
public string Author { get; set; }
93+
}
94+
95+
// BookType.cs
96+
public class BookType : ObjectType<Book>
97+
{
98+
protected override void Configure(IObjectTypeDescriptor<Query> descriptor)
99+
{
100+
descriptor
101+
.Field(f => f.Title)
102+
.Type<StringType>();
103+
104+
descriptor
105+
.Field(f => f.Author)
106+
.Type<StringType>();
107+
}
108+
}
109+
110+
111+
// Startup.cs
112+
public class Startup
113+
{
114+
public void ConfigureServices(IServiceCollection services)
115+
{
116+
services
117+
.AddRouting()
118+
.AddGraphQLServer()
119+
.AddQueryType<QueryType>();
120+
}
121+
122+
// Omitted code for brevity
123+
}
124+
```
125+
126+
</ExampleTabs.Code>
127+
<ExampleTabs.Schema>
128+
129+
```csharp
130+
// Query.cs
131+
public class Query
132+
{
133+
public Task<Book?> GetBookById(
134+
[Service] IMongoCollection<Book> collection,
135+
Guid id)
136+
{
137+
return collection.Find(x => x.Id == id).FirstOrDefaultAsync();
138+
}
139+
}
140+
141+
// Startup.cs
142+
public class Startup
143+
{
144+
public void ConfigureServices(IServiceCollection services)
145+
{
146+
services
147+
.AddRouting()
148+
.AddGraphQLServer()
149+
.AddDocumentFromString(@"
150+
type Query {
151+
bookById(id: Uuid): Book
152+
}
153+
154+
type Book {
155+
title: String
156+
author: String
157+
}
158+
")
159+
.BindComplexType<Query>();
160+
}
161+
162+
// Omitted code for brevity
163+
}
164+
```
165+
166+
</ExampleTabs.Schema>
167+
</ExampleTabs>
10168

11-
Sorry for any inconvenience, and thank you for being patient!
12169

13-
The ChilliCream Team

0 commit comments

Comments
 (0)
Please sign in to comment.