Top Banner
AZURE GraphQL with Entity Framework Core Toni Petrina
32

GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

May 23, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

GraphQL with Entity

Framework Core

Toni Petrina

Page 2: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

Agenda

• What is GraphQL

• GraphQL in action

• Features

• Conjuction with EF

• Future work

Page 3: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

Toni Petrina

Full stack dev, Microsoft MVP

Visma e-conomic a/s

@to_pe

https://github.com/tpetrina

Page 4: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

What is GraphQL?

• A new way of building APIs with strong

emphasis on graph-like querying

• Single endpoint (POST)

• Requires data modeling

Page 5: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

Model –>Ask –> Results{

project(name: "GraphQL") {

tagline

}

}

{

"project": {

"tagline": "A query language for APIs"

}

}

type Project {

name: String

tagline: String

contributors: [User]

}

Page 6: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

Model –>Ask –> Results{

project(name: "GraphQL") {

tagline

}

}

{

"project": {

"tagline": "A query language for APIs"

}

}

type Project {

name: String

tagline: String

contributors: [User]

}

Model Query Result

Page 7: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

Connect to SQL

• We will use EF Core to resolve data

• Mapping of EF models to GraphQL

• Data loader

• Pitfalls

• Why EF? Because it supports SQL and

NoSQL

Page 8: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZUREAZURE

Demo

Page 9: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

Declaring global schemapublic class Demo1Schema : Schema

{

public Demo1Schema(Demo1Query query)

{

Query = query;

}

}

Page 10: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

How do we get data?

• Every defined type needs data resolvers

• Data resolvers can be anything

Page 11: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

Fetching productspublic class Demo1Query : ObjectGraphType {

public Demo1Query(ApplicationDbContext db) {

Field<ListGraphType<DemoProductType>>(

"products",

resolve: context =>

{

return db.Product.ToList();

}

);

}

}

Page 12: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

Fetching productspublic class DemoProductType : ObjectGraphType<DemoProduct>

{

public DemoProductType()

{

Field(i => i.Id);

Field(i => i.Name);

}

}

[Table("Product")]

public class DemoProduct

{

[Key]

public int Id { get; set; }

public string Name { get; set; }

}

Page 13: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

Fetching one productpublic class Demo1Query : ObjectGraphType {

public Demo1Query(ApplicationDbContext db) {

Field<DemoProductType>(

"product",

arguments: new QueryArguments(new QueryArgument<NonNullGraphType<IntGraphType>> { Name ="id" }),

resolve: context => {

var id = context.GetArgument<int>("id");

return db.Product.FirstOrDefault(x => x.Id == id);

}

);

}

}

Page 14: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

Fetching productsField<ListGraphType<DemoProductType>>("products",

arguments: new QueryArguments(new QueryArgument<StringGraphType> { Name = "name" }),

resolve: context =>

{var nameFilter = context.GetArgument<string>("name");

if (string.IsNullOrEmpty(nameFilter))

return db.Product.ToList();

return db.Product

.Where(x => EF.Functions.Like(x.Name, nameFilter))

.ToList();

}

);

Page 15: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

Powerful introspection

• Inspectable by default

• Allows tooling on top

• Generate TypeScript files

• GraphiQL

Page 16: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

Mutations

• Another graph object

• Can return object – reuse graph query

Page 17: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

Mutationpublic class ProductMutation : ObjectGraphType

{

public ProductMutation(ApplicationDbContext db)

{

Field<DemoProductType>(

"createProduct",

arguments: new QueryArguments(

new QueryArgument<NonNullGraphType<CreateProductType>> {Name = "product" }

),

resolve: context =>

{

var product = context.GetArgument<DemoProduct>("product");

db.Product.Add(product);

db.SaveChanges();

return product;

}

);

}

}

Page 18: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

Mutation parampublic class CreateProductType : InputObjectGraphType

{

public CreateProductType()

{

Name = "CreateProduct";

Field<NonNullGraphType<StringGraphType>>("name");

}

}

Page 19: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

Features

• Ask what you need

• One request for multiple resources

• Typed responses

• Evolving API and metrics

• GraphiQL

https://graphql.org

Page 20: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZUREAZURE

Demo - Northwind

Page 21: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

What we have seen

• Mappings, simple and complex

• Deprecating fields

• Complexity

• One to many, many to many

• DataLoader pattern for optimizing N+1

queries

Page 22: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

When do we want to use this?

• Unknown consumer patterns (public

API)

• Underfetching (N+1), overfetching

• Quickly evolving backend/frontend

• Microservices

Page 23: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

GraphQL limitations

• Standardized endpoint (only 1)

• POST vs PUT vs PATCH

• Naming discussions

• Proliferation of special types

• Waiting for endpoint to be extended…

Page 24: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZUREAZURE

Demo – consume from

frontend

Page 25: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

Using Apolloimport ApolloClient, { gql } from 'apollo-boost';

const client = new ApolloClient({

uri: 'https://localhost:5001/api/graphql',

});

client

.query({ query: gql`

{

products {

productName

}

}`})

.then(result => setData(result.data));

Page 26: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZUREAZURE

Demo – Relay connections

Page 27: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

…further features

• Authorization on field level

• Learn type system under the hood

• Union types

• Enums

• Split schema in multiple files

• Generate simple GraphQL for CRUD

• Subscriptions

• Variables, fragments, more…

Page 28: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

Pitfalls

• Authorization – can you access this?

• Errors – How to handle special cases?

• Overfetching on server side

• DDOS waiting to happen?

• Efficient translation?

• Max depth?

Page 29: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

Conclusion

Should you use it?

It depends!

Page 30: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

See more• Hasura https://blog.hasura.io/architecture-of-a-high-performance-graphql-to-sql-

server-58d9944b8a87/

• https://graphql-dotnet.github.io

• https://graphql.org

• https://www.apollographql.com/docs/react/

• https://github.com/Dotnet-Boxed/Templates/blob/master/Docs/GraphQL.md

Page 31: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZUREAZURE

Questions?

Page 32: GraphQL with Entity Framework Core - Home - Experts Live ...€¦ · GraphQL with Entity Framework Core Toni Petrina. AZURE Agenda ... Demo –Relay connections. AZURE …further

AZURE

Thank you!

Please review my session in the Yellenge App!