Dotnet api graphql linux: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Created page with "= Creating a Project = ==Copy the project from RAID array== Sorry no help here == Add the packages== <syntaxhighlight lang="bash"> dotnet add package GraphQL </syntaxhighligh..."
 
Line 14: Line 14:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
dotnet ef database update
dotnet ef database update
</syntaxhighlight>
==Run the Site==
To the site and look at https://localhost:5001/ui/playground
=Creating a Schema=
==Create a Product Graph Type==
Create Fields based on Meta Fields within ObjectGraphType
<syntaxhighlight lang="c#">
    public class ProductType: ObjectGraphType<Product>
    {
        public ProductType()
        {
            Field(t => t.Id);
            Field(t => t.Name).Description("The name of the product");
            Field(t => t.Description);
        }
    }
</syntaxhighlight>
==Create a Carved Rock Query==
Create a Query which looks in the repository (DB) and gets all of the products.
<syntaxhighlight lang="c#">
    public class CarvedRockQuery: ObjectGraphType
    {
        public CarvedRockQuery(ProductRepository productRepository)
        {
            Field<ListGraphType<ProductType>>(
                "products",
                resolve: context => productRepository.GetAll()
            );
        }
    }
</syntaxhighlight>
==Create a Schema for the Carved Rock Query==
This holds the queries the schema supports. In our case the CarvedRockQuery created above
<syntaxhighlight lang="c#">
    public class CarvedRockSchema: Schema
    {
        public CarvedRockSchema(IDependencyResolver resolver): base(resolver)
        {         
            Query = resolver.Resolve<CarvedRockQuery>();
        }
    }
</syntaxhighlight>
</syntaxhighlight>

Revision as of 05:54, 6 August 2020

Creating a Project

Copy the project from RAID array

Sorry no help here

Add the packages

dotnet add package GraphQL

Create Database

Change the connection string

"CarvedRock": "Server=.;Database=CarvedRock;Trusted_Connection=False;User Id=test;Password=nottherealone"

Update the database

dotnet ef database update

Run the Site

To the site and look at https://localhost:5001/ui/playground

Creating a Schema

Create a Product Graph Type

Create Fields based on Meta Fields within ObjectGraphType

    public class ProductType: ObjectGraphType<Product>
    {
        public ProductType()
        {
            Field(t => t.Id);
            Field(t => t.Name).Description("The name of the product");
            Field(t => t.Description);
        }
    }

Create a Carved Rock Query

Create a Query which looks in the repository (DB) and gets all of the products.

    public class CarvedRockQuery: ObjectGraphType
    {
        public CarvedRockQuery(ProductRepository productRepository)
        {
            Field<ListGraphType<ProductType>>(
                "products", 
                resolve: context => productRepository.GetAll()
            );
        }
    }

Create a Schema for the Carved Rock Query

This holds the queries the schema supports. In our case the CarvedRockQuery created above

    public class CarvedRockSchema: Schema
    {
        public CarvedRockSchema(IDependencyResolver resolver): base(resolver)
        {           
            Query = resolver.Resolve<CarvedRockQuery>();
        }
    }