Skip to content

Commit

Permalink
Added Fetching from Database documentation (#3600)
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalSenn authored Apr 27, 2021
1 parent c774d90 commit 79c372d
Showing 1 changed file with 161 additions and 5 deletions.
166 changes: 161 additions & 5 deletions website/src/docs/hotchocolate/fetching-data/fetching-from-databases.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,168 @@
title: "Fetching from Databases"
---

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

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!
In this section, you find a simple example on how you can fetch data from a database and expose it as a GraphQL API.

In case you might need help, check out our slack channel and get immediate help from the core contributors or the community itself.
**HotChocolate is not bound to a specific database, pattern or architecture.**
[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.
You can couple your business logic close to the GraphQL server, or cleanly decouple your domain layer from the GraphQL layer over abstractions.
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.

In this example, we will directly fetch data from MongoDB in a resolver.

# Setting up the Query
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.
If a field is requested, the resolver of the field is called.
The data of this resolver is used for further execution.
If you return a scalar, value (e.g. `string`, `int` ...) the value is serialized and added to the response.
If you return an object, this object is the parent of the resolver in the subtree.

<ExampleTabs>
<ExampleTabs.Annotation>

```csharp
// Query.cs
public class Query
{
public Task<Book?> GetBookById(
[Service] IMongoCollection<Book> collection,
Guid id)
{
return collection.Find(x => x.Id == id).FirstOrDefaultAsync();
}
}

// Book.cs
public class Book
{
public string Title { get; set; }

public string Author { get; set; }
}

// Startup.cs
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services
.AddRouting()
.AddGraphQLServer()
.AddQueryType<Query>();
}

// Omitted code for brevity
}
```

</ExampleTabs.Annotation>
<ExampleTabs.Code>

```csharp
// Query.cs
public class Query
{
public Task<Book?> GetBookById(
[Service] IMongoCollection<Book> collection,
Guid id)
{
return collection.Find(x => x.Id == id).FirstOrDefaultAsync();
}
}

// QueryType.cs
public class QueryType : ObjectType<Query>
{
protected override void Configure(IObjectTypeDescriptor<Query> descriptor)
{
descriptor
.Field(f => f.GetBookById(default!, default!))
.Type<BookType>();
}
}

// Book.cs
public class Book
{
public string Title { get; set; }

public string Author { get; set; }
}

// BookType.cs
public class BookType : ObjectType<Book>
{
protected override void Configure(IObjectTypeDescriptor<Query> descriptor)
{
descriptor
.Field(f => f.Title)
.Type<StringType>();

descriptor
.Field(f => f.Author)
.Type<StringType>();
}
}


// Startup.cs
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services
.AddRouting()
.AddGraphQLServer()
.AddQueryType<QueryType>();
}

// Omitted code for brevity
}
```

</ExampleTabs.Code>
<ExampleTabs.Schema>

```csharp
// Query.cs
public class Query
{
public Task<Book?> GetBookById(
[Service] IMongoCollection<Book> collection,
Guid id)
{
return collection.Find(x => x.Id == id).FirstOrDefaultAsync();
}
}

// Startup.cs
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services
.AddRouting()
.AddGraphQLServer()
.AddDocumentFromString(@"
type Query {
bookById(id: Uuid): Book
}
type Book {
title: String
author: String
}
")
.BindComplexType<Query>();
}

// Omitted code for brevity
}
```

</ExampleTabs.Schema>
</ExampleTabs>

Sorry for any inconvenience, and thank you for being patient!

The ChilliCream Team

0 comments on commit 79c372d

Please sign in to comment.